Exclude per-item usage attribution by default (#63219)

## What changed

Add `usage.attribution` to the `x-codex-exclude` header for Responses HTTP
requests and WebSocket handshakes by default. Threads started with
`experimentalRawEvents` keep receiving per-item attribution in raw responses.
Subagents inherit the policy, and the app server restores it when a thread
resumes with an existing raw-event subscription.

## Testing

Cover the default exclusion, raw completion events with upstream usage,
subagent inheritance, and resuming a closed subagent after both its runtime
and its parent's runtime have unloaded.

GitOrigin-RevId: 0dad92f97606300907f1294f09dd7845d1e6476f
This commit is contained in:
Charlie Marsh
2026-11-23 01:06:41 +00:00
committed by copyberry
parent 9e3479e00a
commit ae6c0c8ac1
13 changed files with 237 additions and 19 deletions
+18 -4
View File
@@ -41,6 +41,7 @@ use response_delivery::ResponseDelivery;
use crate::CodexResponsesHeaders;
use crate::PromptCacheKey;
use crate::UsageAttributionPolicy;
use crate::tools::ExecutedToolCalls;
use async_channel::Sender;
use codex_api::AgentIdentityTelemetry;
@@ -354,6 +355,7 @@ impl RequestRouteTelemetry {
/// call site.
#[derive(Debug, Clone)]
pub struct ModelClient {
usage_attribution_policy: UsageAttributionPolicy,
state: Arc<ModelClientState>,
agent_identity_policy: AgentIdentityAuthPolicy,
prompt_cache_key_policy: PromptCacheKey,
@@ -708,6 +710,7 @@ impl ModelClient {
prompt_cache_key_policy: PromptCacheKey::default(),
session_id_override: None,
codex_responses_headers: None,
usage_attribution_policy: UsageAttributionPolicy::default(),
event_sender: None,
http_client_factory,
restored_history: false,
@@ -749,7 +752,7 @@ impl ModelClient {
prompt_cache_key: PromptCacheKey,
session_id_override: Option<String>,
event_sender: Sender<ProtocolEvent>,
codex_responses_headers: Option<Arc<CodexResponsesHeaders>>,
thread_extension_data: &codex_extension_api::ExtensionData,
api_key_cyber_access_programs: cyber_access_program::ApiKeyCyberAccessPrograms,
required_cyber_access_programs: Option<
Arc<cyber_access_program::RequiredCyberAccessPrograms>,
@@ -758,7 +761,11 @@ impl ModelClient {
self.prompt_cache_key_policy = prompt_cache_key;
self.session_id_override = session_id_override;
self.event_sender = Some(event_sender);
self.codex_responses_headers = codex_responses_headers;
self.codex_responses_headers = thread_extension_data.get::<CodexResponsesHeaders>();
self.usage_attribution_policy = thread_extension_data
.get::<UsageAttributionPolicy>()
.map(|policy| *policy)
.unwrap_or_default();
self.api_key_cyber_access_programs = api_key_cyber_access_programs;
self.required_cyber_access_programs = required_cyber_access_programs;
self
@@ -1690,6 +1697,7 @@ impl ModelClient {
) -> ApiHeaderMap {
let mut headers = build_responses_headers(
self.state.beta_features_header.as_deref(),
self.usage_attribution_policy,
/*turn_state*/ None,
);
add_originator_header(&mut headers, self.state.originator.as_str());
@@ -1766,6 +1774,7 @@ impl<'a> ResponsesHttpExecutor<'a> {
extra_headers: {
let mut headers = build_responses_headers(
self.client.state.beta_features_header.as_deref(),
self.client.usage_attribution_policy,
Some(turn_state),
);
add_originator_header(&mut headers, self.client.state.originator.as_str());
@@ -2947,13 +2956,18 @@ fn is_guardian_reviewer(responses_headers: &ApiHeaderMap) -> bool {
///
/// - `x-codex-beta-features`: comma-separated beta feature keys enabled for the session.
/// - `x-codex-turn-state`: sticky routing token captured earlier in the turn.
/// - `x-codex-exclude` asks the server to omit the echoed tool catalog.
/// - `x-codex-exclude`: response fields the client can omit from the stream.
fn build_responses_headers(
beta_features_header: Option<&str>,
usage_attribution_policy: UsageAttributionPolicy,
turn_state: Option<&Arc<OnceLock<String>>>,
) -> ApiHeaderMap {
let mut headers = ApiHeaderMap::new();
headers.insert("x-codex-exclude", HeaderValue::from_static("tools"));
let exclusions = match usage_attribution_policy {
UsageAttributionPolicy::Exclude => "tools,usage.attribution",
UsageAttributionPolicy::Retain => "tools",
};
headers.insert("x-codex-exclude", HeaderValue::from_static(exclusions));
if let Some(value) = beta_features_header
&& !value.is_empty()
&& let Ok(header_value) = HeaderValue::from_str(value)
+1
View File
@@ -15,6 +15,7 @@ mod realtime_conversation;
mod realtime_history;
mod responses_headers;
pub use responses_headers::CodexResponsesHeaders;
pub use responses_headers::UsageAttributionPolicy;
mod responses_metadata;
mod responses_retry;
pub(crate) mod session;
+11 -2
View File
@@ -1,8 +1,17 @@
//! Header overrides for host-created threads on the Codex Responses endpoint.
//! Headers are scoped to a model and rechecked against current auth on each attempt.
//! Host-supplied header settings for threads on the Codex Responses endpoint.
use http::HeaderMap;
/// Controls delivery of per-item attribution requested by the Codex backend for
/// accounting. Seed in `StartThreadOptions::thread_extension_init` to set the
/// policy for a thread and its subagents.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum UsageAttributionPolicy {
#[default]
Exclude,
Retain,
}
/// Seed in `StartThreadOptions::thread_extension_init` to supply Responses headers.
///
/// Applied only to the selected model on the OpenAI provider with Codex backend
+1 -3
View File
@@ -1869,8 +1869,6 @@ impl Session {
};
let executed_tool_calls =
crate::state::ExecutedToolCalls::new(&config.features, &initial_history);
let codex_responses_headers =
thread_extension_data.get::<crate::CodexResponsesHeaders>();
// Ephemeral title requests use request-level effort even when managed settings
// enable overrides. The client-supplied tag selects cache behavior, not permissions.
let title_request = config.ephemeral
@@ -1926,7 +1924,7 @@ impl Session {
)
.or(fork_cache_key),
tx_event.clone(),
codex_responses_headers,
&thread_extension_data,
crate::cyber_access_program::ApiKeyCyberAccessPrograms::from_config(&config),
required_cyber_access_programs,
);
+11
View File
@@ -2698,6 +2698,17 @@ impl ThreadManagerState {
}
let inherited_environments = captured_environments.or(inherited_environments);
let session_source = session_source.unwrap_or_else(|| self.session_source.clone());
if let Some(parent_id) = parent_thread_id
&& thread_extension_init
.get::<crate::UsageAttributionPolicy>()
.is_none()
&& let Some(parent) = self.threads.read().await.get(&parent_id)
&& let Some(policy) = parent
.thread_extension_data()
.get::<crate::UsageAttributionPolicy>()
{
thread_extension_init.insert(*policy);
}
if thread_extension_init.get::<InheritedSessionId>().is_some()
&& (session_source.is_non_root_agent()
|| !matches!(