mirror of
https://github.com/openai/codex.git
synced 2026-11-16 06:54:36 +00:00
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:
1 parent
73a1148c9c
commit
d3ffbbed5a
13 files changed
+472
-9
No files matched your search
@@ -9,6 +9,8 @@ mod decision;
|
||||
mod feedback;
|
||||
mod metrics;
|
||||
mod prompt;
|
||||
mod request_budget;
|
||||
pub(crate) use request_budget::observe as observe_guardian_request;
|
||||
mod review;
|
||||
mod review_session;
|
||||
mod reviewer_config;
|
||||
|
||||
@@ -210,9 +210,21 @@ pub(crate) async fn build_guardian_prompt_items_with_parent_turn(
|
||||
if transcript_entries.is_empty() {
|
||||
transcript.items.push(placeholder.to_owned());
|
||||
}
|
||||
let items = sections
|
||||
.compose(presentation, transcript)?
|
||||
.into_user_inputs()?;
|
||||
let context = sections.compose(presentation, transcript)?;
|
||||
for (section, cost) in context.section_costs() {
|
||||
for (measurement, value) in cost.measurements() {
|
||||
session.services.session_telemetry.histogram(
|
||||
codex_guardian_context::SECTION_COST_METRIC,
|
||||
i64::try_from(value).unwrap_or(i64::MAX),
|
||||
&[
|
||||
("target", "sync"),
|
||||
("section", section),
|
||||
("measurement", measurement),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
let items = context.into_user_inputs()?;
|
||||
Ok(GuardianPromptItems {
|
||||
items,
|
||||
transcript_cursor,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
//! Measures the complete synchronous request after image admission and wire
|
||||
//! prefix assembly. Includes reused history, tool definitions and output format.
|
||||
|
||||
use codex_guardian_context::REQUEST_TOKENS_METRIC;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
|
||||
use crate::context_manager::estimate_item_token_count;
|
||||
use codex_api::ResponsesApiRequest;
|
||||
use codex_otel::SessionTelemetry;
|
||||
|
||||
pub(crate) fn observe(telemetry: &SessionTelemetry, request: &ResponsesApiRequest) -> usize {
|
||||
let input = request
|
||||
.input
|
||||
.iter()
|
||||
.map(estimate_item_token_count)
|
||||
.fold(0i64, i64::saturating_add);
|
||||
let instructions = TruncationPolicy::Bytes(request.instructions.len()).token_budget();
|
||||
let metadata = serde_json::to_vec(&(&request.tools, &request.text))
|
||||
.map(|bytes| TruncationPolicy::Bytes(bytes.len()).token_budget())
|
||||
.unwrap_or(usize::MAX);
|
||||
let total = usize::try_from(input)
|
||||
.unwrap_or(usize::MAX)
|
||||
.saturating_add(instructions)
|
||||
.saturating_add(metadata);
|
||||
// The assembled input already includes inherited history and the current
|
||||
// review. Do not report a guessed old/new split after context injection.
|
||||
telemetry.histogram(
|
||||
REQUEST_TOKENS_METRIC,
|
||||
i64::try_from(total).unwrap_or(i64::MAX),
|
||||
&[("target", "sync"), ("component", "total")],
|
||||
);
|
||||
total
|
||||
}
|
||||
Reference in new issue
Block a user