Add Guardian context cost and request token telemetry (#44164)

## What changed

- Record per-section text bytes, estimated text tokens, image bytes, and image counts for synchronous reviews and asynchronous scoring without logging evidence payloads.
- Emit estimated request tokens through `codex.guardian.context.request_tokens`. Synchronous estimates include assembled history, instructions, tool definitions, and output format, and measure the full logical request before WebSocket delta generation. Asynchronous estimates cover the assembled input.
- Add shared context budgeting helpers, including conservative image token reservations independent of encoded payload size and model-aware input limit calculation.

## Testing

Add coverage for separate text and image accounting, image estimates independent of encoded size, and section estimates that bound delivered messages. Extend asynchronous scorer and app-server tests to verify cost metric emission.

GitOrigin-RevId: aed45ecd9c23706f88caa51f2a4f2c77872d3bdb
This commit is contained in:
felixxia-oai authored and copyberry committed 2026-09-09 14:48:08 +00:00
1 parent 73a1148c9c
commit d3ffbbed5a
13 files changed
+472 -9

No files matched your search

+9
View File
@@ -1640,6 +1640,9 @@ impl ModelClientSession {
);
self.client
.prepare_response_items_for_request(&mut request.input);
if crate::guardian::is_basic_session_source(&self.client.state.session_source) {
crate::guardian::observe_guardian_request(session_telemetry, &request);
}
let request_session_telemetry =
session_telemetry_for_request(session_telemetry, &request);
let inference_trace_attempt = inference_trace.start_attempt();
@@ -1830,6 +1833,12 @@ impl ModelClientSession {
Err(err) => return Err(provider.map_api_error(err)),
}
// Measure the complete logical request, not only the websocket delta.
if !warmup
&& crate::guardian::is_basic_session_source(&self.client.state.session_source)
{
crate::guardian::observe_guardian_request(session_telemetry, &request);
}
let (incremental_request, previous_response_id_from_untraced_warmup) =
self.prepare_websocket_request(&request);
let inference_trace_attempt = if warmup {