-
Notifications
You must be signed in to change notification settings - Fork 80
feat(context): track agent.messages token size #790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,12 @@ export interface AgentMetricsData { | |
| * Per-tool execution metrics keyed by tool name. | ||
| */ | ||
| toolMetrics: Record<string, ToolMetricsData> | ||
|
|
||
| /** | ||
| * The most recent input token count from the last model invocation. | ||
| * Represents the current context window utilization. | ||
| */ | ||
| latestContextSize?: number | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: The TSDoc says "context window utilization" but /**
* The most recent input token count from the last model invocation.
* This is a lagging indicator (post-call) that tracks the `inputTokens` value
* reported by the model provider, useful for threshold-based context management decisions.
*/This matches the Python PR description's framing: "a lagging indicator (post-call), not a pre-call estimate." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the update commit — the |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -171,12 +177,20 @@ export class AgentMetrics implements JSONSerializable<AgentMetricsData> { | |
| */ | ||
| readonly toolMetrics: Record<string, ToolMetricsData> | ||
|
|
||
| /** | ||
| * 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 latestContextSize: number | undefined | ||
|
|
||
| constructor(data?: Partial<AgentMetricsData>) { | ||
| this.cycleCount = data?.cycleCount ?? 0 | ||
| this.accumulatedUsage = data?.accumulatedUsage ?? createEmptyUsage() | ||
| this.accumulatedMetrics = data?.accumulatedMetrics ?? { latencyMs: 0 } | ||
| this.agentInvocations = data?.agentInvocations ?? [] | ||
| this.toolMetrics = data?.toolMetrics ?? {} | ||
| this.latestContextSize = data?.latestContextSize | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -236,6 +250,7 @@ export class AgentMetrics implements JSONSerializable<AgentMetricsData> { | |
| accumulatedMetrics: this.accumulatedMetrics, | ||
| agentInvocations: this.agentInvocations, | ||
| toolMetrics: this.toolMetrics, | ||
| ...(this.latestContextSize !== undefined && { latestContextSize: this.latestContextSize }), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -277,6 +292,11 @@ export class Meter { | |
| */ | ||
| private readonly _toolMetrics: Record<string, ToolMetricsData> = {} | ||
|
|
||
| /** | ||
| * The most recent input token count from the last model invocation. | ||
| */ | ||
| private _latestContextSize: number | undefined | ||
|
|
||
| // OTEL instruments (no-op when no MeterProvider is registered) | ||
| private readonly _otelMeter: OtelMeter | ||
| private readonly _otelCycleCounter: Counter | ||
|
|
@@ -335,6 +355,7 @@ export class Meter { | |
| * Creates a new InvocationMetricsData entry for per-invocation metrics. | ||
| */ | ||
| startNewInvocation(): void { | ||
| this._latestContextSize = undefined | ||
| this._agentInvocations.push({ | ||
| cycles: [], | ||
| usage: createEmptyUsage(), | ||
|
|
@@ -438,6 +459,7 @@ export class Meter { | |
| accumulatedMetrics: this._accumulatedMetrics, | ||
| agentInvocations: this._agentInvocations, | ||
| toolMetrics: this._toolMetrics, | ||
| ...(this._latestContextSize !== undefined && { latestContextSize: this._latestContextSize }), | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -474,6 +496,7 @@ export class Meter { | |
| */ | ||
| private _updateUsage(usage: Usage): void { | ||
| accumulateUsage(this._accumulatedUsage, usage) | ||
| this._latestContextSize = usage.inputTokens | ||
|
|
||
| this._otelInputTokens.add(usage.inputTokens) | ||
| this._otelOutputTokens.add(usage.outputTokens) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.