metrics

package
v1.3.72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatDuration added in v1.3.66

func FormatDuration(d time.Duration) string

FormatDuration formats a duration for human-readable display.

func FormatTurnDigest added in v1.3.66

func FormatTurnDigest(lang string, turn TurnSummary) string

FormatTurnDigest produces a single-line summary of a turn's metrics. lang is a BCP-47 language code (e.g. "en", "zh-CN").

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector receives MetricEvents on a buffered channel and persists them asynchronously via a callback. Events are dropped (never blocked) if the channel is full, so the agent is never slowed down.

func NewCollector

func NewCollector(ctx context.Context, capacity int, persist func(MetricEvent)) *Collector

NewCollector starts a background goroutine that drains ch and calls persist for each event. cap is the channel buffer size (256 is a good default).

func (*Collector) Emit

func (c *Collector) Emit(m MetricEvent)

Emit sends a metric event to the collector. Non-blocking: drops if full.

func (*Collector) Flush added in v1.3.46

func (c *Collector) Flush()

Flush blocks until all currently queued events have been persisted.

func (*Collector) Stop

func (c *Collector) Stop()

Stop signals the collector goroutine to drain and exit.

type MetricEvent

type MetricEvent struct {
	Timestamp time.Time `json:"timestamp"`
	TurnIndex int       `json:"turn_index"`
	Type      string    `json:"type"` // "llm" or "tool"

	// LLM metrics
	TTFT      time.Duration `json:"ttft,omitempty"`       // time to first text/reasoning token
	ThinkTime time.Duration `json:"think_time,omitempty"` // cumulative reasoning/thinking duration
	Duration  time.Duration `json:"duration,omitempty"`   // total LLM call wall time

	// Tool metrics
	ToolName     string        `json:"tool_name,omitempty"`
	ToolSuccess  bool          `json:"tool_success,omitempty"`
	ToolError    string        `json:"tool_error,omitempty"`
	ToolDuration time.Duration `json:"tool_duration,omitempty"`

	// Metadata
	Model    string `json:"model,omitempty"`
	Vendor   string `json:"vendor,omitempty"`
	Endpoint string `json:"endpoint,omitempty"`
}

MetricEvent records a single performance measurement — either an LLM API call or a tool execution. Designed to be fire-and-forget: the agent emits these via a callback, and the caller persists them asynchronously.

type SessionSummary added in v1.3.44

type SessionSummary struct {
	TurnCount        int
	LLMCallCount     int
	ToolCallCount    int
	ToolFailureCount int

	AvgTTFT     time.Duration
	P95TTFT     time.Duration
	AvgDuration time.Duration
	P95Duration time.Duration
	AvgThink    time.Duration

	SlowTools []ToolSummary
	Turns     []TurnSummary
}

func Summarize added in v1.3.44

func Summarize(events []MetricEvent) SessionSummary

func (SessionSummary) HasData added in v1.3.44

func (s SessionSummary) HasData() bool

func (SessionSummary) ToolFailureRate added in v1.3.44

func (s SessionSummary) ToolFailureRate() int

type ToolSummary added in v1.3.44

type ToolSummary struct {
	Name        string
	Calls       int
	Failures    int
	AvgDuration time.Duration
	MaxDuration time.Duration
}

type TurnSummary added in v1.3.44

type TurnSummary struct {
	TurnIndex           int
	LLMCallCount        int
	ToolCallCount       int
	ToolFailureCount    int
	TTFT                time.Duration
	Duration            time.Duration
	ThinkTime           time.Duration
	SlowestTool         string
	SlowestToolDuration time.Duration
}

func TurnSummaryForIndex added in v1.3.46

func TurnSummaryForIndex(events []MetricEvent, turnIndex int) (TurnSummary, bool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL