AgentMetrics
Defined in: src/telemetry/meter.ts:167
Read-only snapshot of aggregated agent metrics.
Returned by Meter.metrics and stored on AgentResult. Provides access to cycle counts, tool usage, token consumption, and per-invocation breakdowns. Supports serialization via toJSON.
Example
Section titled “Example”const result = await agent.invoke('Hello')console.log(result.metrics?.cycleCount)console.log(result.metrics?.totalDuration)console.log(result.metrics?.accumulatedData)console.log(result.metrics?.toolMetrics)console.log(JSON.stringify(result.metrics))Implements
Section titled “Implements”JSONSerializable<AgentMetricsData>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AgentMetrics(data?): AgentMetrics;Defined in: src/telemetry/meter.ts:215
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
data? | Partial<AgentMetricsData> |
Returns
Section titled “Returns”AgentMetrics
Properties
Section titled “Properties”cycleCount
Section titled “cycleCount”readonly cycleCount: number;Defined in: src/telemetry/meter.ts:171
Total number of agent loop cycles executed across all invocations.
accumulatedUsage
Section titled “accumulatedUsage”readonly accumulatedUsage: Usage;Defined in: src/telemetry/meter.ts:176
Accumulated token usage across all model invocations.
accumulatedMetrics
Section titled “accumulatedMetrics”readonly accumulatedMetrics: Metrics;Defined in: src/telemetry/meter.ts:181
Accumulated performance metrics across all model invocations.
agentInvocations
Section titled “agentInvocations”readonly agentInvocations: InvocationMetricsData[];Defined in: src/telemetry/meter.ts:188
Per-invocation metrics for recent invocations. Only the most recent 50 entries are retained to prevent unbounded memory growth. For full history, collect latestAgentInvocation from each AgentResult.
toolMetrics
Section titled “toolMetrics”readonly toolMetrics: Record<string, ToolMetricsData>;Defined in: src/telemetry/meter.ts:193
Per-tool execution metrics keyed by tool name.
latestContextSize
Section titled “latestContextSize”readonly latestContextSize: number;Defined in: src/telemetry/meter.ts:200
The total prompt the model processed on the last invocation, including cached tokens.
Represents the current context window utilization.
Returns undefined when no invocations have occurred.
projectedContextSize
Section titled “projectedContextSize”readonly projectedContextSize: number;Defined in: src/telemetry/meter.ts:208
Projected context size for the next model call (total prompt including cached tokens plus the
generated output from the last call).
Represents the baseline token count the next invocation will start with.
Returns undefined when no invocations have occurred.
totalDuration
Section titled “totalDuration”readonly totalDuration: number;Defined in: src/telemetry/meter.ts:213
Total duration of all cycles across all invocations in milliseconds.
Accessors
Section titled “Accessors”latestAgentInvocation
Section titled “latestAgentInvocation”Get Signature
Section titled “Get Signature”get latestAgentInvocation(): InvocationMetricsData;Defined in: src/telemetry/meter.ts:229
The most recent agent invocation, or undefined if none exist.
Returns
Section titled “Returns”InvocationMetricsData
accumulatedData
Section titled “accumulatedData”Get Signature
Section titled “Get Signature”get accumulatedData(): { usage: Usage; metrics: Metrics;};Defined in: src/telemetry/meter.ts:236
Accumulated usage and performance metrics across all model invocations.
Returns
Section titled “Returns”{ usage: Usage; metrics: Metrics;}| Name | Type | Defined in |
|---|---|---|
usage | Usage | src/telemetry/meter.ts:236 |
metrics | Metrics | src/telemetry/meter.ts:236 |
averageCycleTime
Section titled “averageCycleTime”Get Signature
Section titled “Get Signature”get averageCycleTime(): number;Defined in: src/telemetry/meter.ts:243
Average cycle duration in milliseconds, or 0 if no cycles exist.
Returns
Section titled “Returns”number
toolUsage
Section titled “toolUsage”Get Signature
Section titled “Get Signature”get toolUsage(): Record<string, ToolMetricsData & { averageTime: number; successRate: number;}>;Defined in: src/telemetry/meter.ts:250
Per-tool execution statistics with computed averages and rates.
Returns
Section titled “Returns”Record<string, ToolMetricsData & {
averageTime: number;
successRate: number;
}>
Methods
Section titled “Methods”toJSON()
Section titled “toJSON()”toJSON(): AgentMetricsData;Defined in: src/telemetry/meter.ts:268
Returns a JSON-serializable representation of all collected metrics. Called automatically by JSON.stringify().
Returns
Section titled “Returns”AgentMetricsData
A plain object suitable for round-trip serialization
Implementation of
Section titled “Implementation of”JSONSerializable.toJSON