Expose response usage metadata in completion events (#41087)

## What changed

- Parse optional `usage_metadata.amount` values from Responses API completion events without converting their string representation.
- Propagate the metadata through SSE and WebSocket streams, regular turns, and remote compaction.
- Add `usageMetadata` to the app-server `rawResponse/completed` notification and its generated schemas.

## Testing

- Cover missing, null, zero, and high-precision amounts, including completion events emitted during compaction.

GitOrigin-RevId: b16d81e8350eab0dc7ff613d0dc9dbed73041713
This commit is contained in:
Dylan Hurd authored and copyberry committed 2026-08-27 07:50:05 +00:00
1 parent b592a0bfed
commit 2c4a95736b
25 files changed
+250 -18

No files matched your search

@@ -1841,6 +1841,23 @@ pub struct RawResponseCompletedNotification {
pub turn_id: String,
pub response_id: String,
pub usage: Option<TokenUsageBreakdown>,
pub usage_metadata: Option<ResponseUsageMetadata>,
}
/// Usage metadata reported for one upstream response.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export_to = "v2/")]
pub struct ResponseUsageMetadata {
pub amount: Option<String>,
}
impl From<codex_protocol::ResponseUsageMetadata> for ResponseUsageMetadata {
fn from(value: codex_protocol::ResponseUsageMetadata) -> Self {
Self {
amount: value.amount,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS)]