Expose assistant content token counts in completion notifications (#59041)

## What changed

Add optional `contentTokenCount` metadata to `item/completed` notifications, carrying the producer-supplied token count for each completed assistant message in default and plan modes. Counts include any sampled footer and are `null` when unavailable.

Keep this telemetry separate from stored thread items and omit it from subsequent model requests. Update the protocol schemas and generated TypeScript and Python types.

## Testing

Add coverage for per-message counts, including zero, in default and plan modes; missing and malformed metadata; backward-compatible notification decoding; and exclusion from subsequent model requests.

GitOrigin-RevId: 35973037a42650fa2d7569da8e88cb81215cf971
This commit is contained in:
aojiao-oai
2026-11-03 02:26:29 +00:00
committed by copyberry
parent e839b828fb
commit 16ddd538bd
48 changed files with 443 additions and 39 deletions
@@ -1505,6 +1505,12 @@ pub struct ItemCompletedNotification {
pub item: ThreadItem,
pub thread_id: String,
pub turn_id: String,
/// Native content tokens for the completed assistant message, including a sampled footer.
/// Null when unavailable. Completion telemetry is not part of the stored thread item.
#[serde(default)]
#[ts(optional)]
#[ts(type = "number | null")]
pub content_token_count: Option<u64>,
/// Unix timestamp (in milliseconds) when this item lifecycle completed.
#[ts(type = "number")]
pub completed_at_ms: i64,
@@ -1520,6 +1526,7 @@ impl ItemCompletedNotification {
Self {
thread_id,
turn_id,
content_token_count: None,
completed_at_ms,
item: ThreadItem::completed(payload, started_at_ms, completed_at_ms),
}