Skip to content

AgentMetrics

Defined in: src/telemetry/meter.ts:166

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.

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))
  • JSONSerializable<AgentMetricsData>
new AgentMetrics(data?): AgentMetrics;

Defined in: src/telemetry/meter.ts:213

ParameterType
data?Partial<AgentMetricsData>

AgentMetrics

readonly cycleCount: number;

Defined in: src/telemetry/meter.ts:170

Total number of agent loop cycles executed across all invocations.


readonly accumulatedUsage: Usage;

Defined in: src/telemetry/meter.ts:175

Accumulated token usage across all model invocations.


readonly accumulatedMetrics: Metrics;

Defined in: src/telemetry/meter.ts:180

Accumulated performance metrics across all model invocations.


readonly agentInvocations: InvocationMetricsData[];

Defined in: src/telemetry/meter.ts:187

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.


readonly toolMetrics: Record<string, ToolMetricsData>;

Defined in: src/telemetry/meter.ts:192

Per-tool execution metrics keyed by tool name.


readonly latestContextSize: number;

Defined in: src/telemetry/meter.ts:199

The most recent input token count from the last model invocation. Represents the current context window utilization. Returns undefined when no invocations have occurred.


readonly projectedContextSize: number;

Defined in: src/telemetry/meter.ts:206

Projected context size for the next model call (inputTokens + outputTokens from the last call). Represents the baseline token count the next invocation will start with. Returns undefined when no invocations have occurred.


readonly totalDuration: number;

Defined in: src/telemetry/meter.ts:211

Total duration of all cycles across all invocations in milliseconds.

get latestAgentInvocation(): InvocationMetricsData;

Defined in: src/telemetry/meter.ts:227

The most recent agent invocation, or undefined if none exist.

InvocationMetricsData


get accumulatedData(): {
usage: Usage;
metrics: Metrics;
};

Defined in: src/telemetry/meter.ts:234

Accumulated usage and performance metrics across all model invocations.

{
usage: Usage;
metrics: Metrics;
}
NameTypeDefined in
usageUsagesrc/telemetry/meter.ts:234
metricsMetricssrc/telemetry/meter.ts:234

get averageCycleTime(): number;

Defined in: src/telemetry/meter.ts:241

Average cycle duration in milliseconds, or 0 if no cycles exist.

number


get toolUsage(): Record<string, ToolMetricsData & {
averageTime: number;
successRate: number;
}>;

Defined in: src/telemetry/meter.ts:248

Per-tool execution statistics with computed averages and rates.

Record<string, ToolMetricsData & { averageTime: number; successRate: number; }>

toJSON(): AgentMetricsData;

Defined in: src/telemetry/meter.ts:266

Returns a JSON-serializable representation of all collected metrics. Called automatically by JSON.stringify().

AgentMetricsData

A plain object suitable for round-trip serialization

JSONSerializable.toJSON