Documentation
¶
Overview ¶
Package agentmeter provides framework-agnostic observability for LLM agent runs. It records reasoning traces, token usage, tool calls, and estimated costs without introducing any external dependencies in the core package.
Index ¶
Constants ¶
const DefaultMaxHistory = 100
DefaultMaxHistory is the default cap on completed-run snapshots retained in History.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AgentStep ¶
type AgentStep struct {
// Role is a free descriptive label set by the client (e.g. "model", "tool",
// "retrieval"). It appears in headers and logs but does not drive rendering
// or counter gating — use Cluster for that.
Role StepRole
// Cluster drives display and counter gating. Set to one of the Cluster*
// constants. ClusterCognitive increments ModelCalls; ClusterAction renders
// as a tool result; ClusterMessage and ClusterError have dedicated styles.
Cluster StepCluster
// AgentName is the name of the agent that performed this step.
AgentName string
// Provider identifies the API provider serving the model
// (e.g. "openai", "anthropic", "google", "azure_openai", "aws_bedrock").
// Follows the gen_ai.system semantic convention from OpenTelemetry.
// Optional: leave empty if the provider is unambiguous from ModelID in your setup.
// Explicit over inferred — the same ModelID can be served by multiple providers
// (e.g. "claude-3-5-sonnet" via Anthropic direct, AWS Bedrock, or Vertex AI).
Provider string
// ModelID is the model identifier (e.g. "gpt-4o"). Non-empty for cognitive steps.
ModelID string
// Content holds the primary text output (model response text or tool result).
Content string
// ThinkingContent holds chain-of-thought text for thinking steps.
ThinkingContent string
// ToolName is the name of the tool invoked. Non-empty for action steps.
ToolName string
// ToolInput is the serialised input passed to the tool.
ToolInput string
// ToolCallID is the provider-assigned correlation ID linking a tool call
// in an assistant message to its corresponding tool result step.
ToolCallID string
// ToolCalls is a list of tool-call summaries (e.g. "search({q:\"go\"})") for
// model steps that dispatched one or more tools. Empty for other step types.
ToolCalls []string
// Usage records token counts for model and thinking steps; zero for tool steps.
Usage TokenUsage
// StartedAt is the wall-clock time at which this step began.
// If Duration is zero and StartedAt is non-zero, Record() auto-computes
// Duration = time.Since(StartedAt). Set this immediately before issuing
// the model or tool call — before any blocking I/O — for accurate timing.
StartedAt time.Time
// Duration is the wall-clock time spent in this step.
// If set explicitly it takes precedence over StartedAt-based auto-calculation.
Duration time.Duration
}
AgentStep is an immutable record of one step in an agent run.
type CostFunc ¶
type CostFunc func(TokenSummary) float64
CostFunc computes the estimated cost in USD for a given TokenSummary. Implementations should be pure functions with no side effects. A nil CostFunc is valid; Meter will skip cost computation in that case.
type Meter ¶
type Meter struct {
// contains filtered or unexported fields
}
Meter records the reasoning trace of one or more agent runs and accumulates a bounded history of completed runs. Safe for concurrent use.
func New ¶
New creates a Meter instance with the supplied options applied. It is safe to call New with no options; all settings have safe defaults.
func (*Meter) ClearHistory ¶
func (t *Meter) ClearHistory()
ClearHistory removes all completed-run snapshots from memory.
func (*Meter) Finalize ¶
func (t *Meter) Finalize()
Finalize marks the current run complete and appends a Snapshot to history. Calling it more than once on the same run is a no-op.
func (*Meter) History ¶
History returns a copy of all completed runs in chronological order. Runs are added by Finalize(). The slice is bounded by the MaxHistory option (default 100). Use ClearHistory to reset it.
func (*Meter) Record ¶
Record appends s to the current run and updates token aggregates. Duration is auto-computed from StartedAt if not set explicitly.
func (*Meter) Reset ¶
Reset clears the current run state and prepares Meter to record a new run. label is a human-readable identifier for this run (e.g. "conversation-42", "search-agent-run-3"). It is stored in Snapshot.Label and does not affect how individual steps are recorded. Steps carry their own AgentName field. Reset does not clear history.
type Option ¶
type Option func(*config)
Option is a functional option that configures a Meter instance. Options are applied in the order they are passed to New().
func WithCostFunc ¶
WithCostFunc sets the CostFunc used to estimate cost in USD for each run. Pass nil to disable cost estimation (the default).
func WithMaxHistory ¶
WithMaxHistory sets the maximum number of completed runs to retain in history. n must be positive; zero or negative values are ignored and the default is kept.
type Snapshot ¶
type Snapshot struct {
// Label is the run identifier set by Reset(label). It identifies the run,
// not the agents within it. Individual steps carry their own AgentName.
Label string
// Steps is a deep copy of the reasoning steps recorded so far.
Steps []AgentStep
// TokenSummary is an aggregated token and cost summary for this run.
TokenSummary TokenSummary
// TotalDuration is the accumulated wall-clock time across all steps in this snapshot.
TotalDuration time.Duration
}
Snapshot is a point-in-time, immutable view of an agent run. It is returned by Meter.Snapshot() and stored in History(). Unlike Meter, a Snapshot carries no mutex and is safe to share freely.
type StepCluster ¶
type StepCluster string
StepCluster is the stable rendering and routing category for a step. It is the library's fixed vocabulary; Role is owned by the client. Use one of the Cluster* constants when recording a step.
const ( // ClusterCognitive covers model inference and extended-thinking steps. // Steps with this cluster increment ModelCalls and are rendered with // thinking/plan/call labels. ClusterCognitive StepCluster = "cognitive" // ClusterAction covers tool calls and external side-effects. ClusterAction StepCluster = "action" // ClusterMessage covers text responses directed at the user. ClusterMessage StepCluster = "message" // ClusterError covers failures, exceptions, and retries. ClusterError StepCluster = "error" )
type StepRole ¶
type StepRole string
StepRole is a free-form descriptive label for a reasoning step. The library defines the type but no constants — clients supply whatever role labels make sense for their framework or application (e.g. "model", "tool", "retrieval", "rerank"). Rendering and counter gating are driven by StepCluster, not by Role.
type TokenSummary ¶
type TokenSummary struct {
// ByModel holds token usage aggregated per model ID.
ByModel map[string]TokenUsage
// ModelCalls is the number of ClusterCognitive steps recorded.
ModelCalls int
// EstimatedCostUSD is the cost computed by CostFunc at Snapshot time.
EstimatedCostUSD float64
}
TokenSummary aggregates multiple usages into a final report.
func (TokenSummary) AggregateTokenUsage ¶
func (s TokenSummary) AggregateTokenUsage() TokenUsage
AggregateTokenUsage returns the sum of all per-model token usage.
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int
CompletionTokens int
TotalTokens int
CachedInputTokens int
CacheWriteTokens int
ReasoningTokens int
}
TokenUsage captures raw token counts. It is the "source of truth" for what happened in a single model interaction.
func (TokenUsage) Add ¶
func (u TokenUsage) Add(other TokenUsage) TokenUsage
Add returns a NEW TokenUsage that is the sum of u and other.
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
eino
module
|
|
|
examples
|
|
|
basic
command
Package main is the minimal agentmeter example.
|
Package main is the minimal agentmeter example. |
|
custom_cost
command
|
|
|
history
command
Package main demonstrates multi-run history with agentmeter.
|
Package main demonstrates multi-run history with agentmeter. |
|
mixed_pricing
command
Package main demonstrates per-model cost tracking with agentmeter.
|
Package main demonstrates per-model cost tracking with agentmeter. |
|
terminal_output
command
Package main demonstrates the terminal rendering options of agentmeter.
|
Package main demonstrates the terminal rendering options of agentmeter. |
|
Package reasoning provides terminal rendering utilities for agentmeter traces.
|
Package reasoning provides terminal rendering utilities for agentmeter traces. |

