Add response processing after completion and usage accounting (#61577)

## Why

Streaming can dispatch tools before a response finishes and its usage is accounted for. Allow callers to wait for completion and synchronous post-hooks so budget enforcement can block tools from the response that exhausts the budget.

## What changed

- Add `responses.process_mode = "after_complete_response"` to buffer response items and release them in order after completion, synchronous post-hooks, and usage accounting. Keep `streaming` as the default and deliver safety-buffering notifications immediately.
- Track whether response usage has already been charged to avoid duplicate accounting, including during compaction.
- Stop new tools across the agent tree when the rollout budget is exhausted. Coordinate steering with accounting and prevent buffered tool calls from dispatching after steering or cancellation.
- Bound buffered responses to 65,535 events and reject overflow.

## Testing

Add configuration coverage and extend integration tests for buffered output ordering, synchronous hooks, interrupted responses and resume, steering, invalid usage, and budget exhaustion preventing tool dispatch.

GitOrigin-RevId: bd06811cfd50cb701babe6353465c57dfed2a37a
This commit is contained in:
tongzhou wang
2026-11-14 23:50:12 +00:00
committed by copyberry
parent 81b995a6c9
commit 92c1a4dc2e
32 changed files with 780 additions and 259 deletions
+11 -4
View File
@@ -7,6 +7,7 @@ use crate::Prompt;
use crate::client::ModelClientSession;
use crate::client::SamplingOptions;
use crate::client_common::ResponseEvent;
use crate::client_common::SamplingEvent;
use crate::compact_pending_input::is_retained_pending_mail;
use crate::compact_remote_v2::retained_client_items;
use crate::context::BaseInstructionsFragment;
@@ -250,6 +251,8 @@ async fn run_compact_task_inner(
CompactionAnalyticsDetails::default(),
)
.await;
// Keep an installed checkpoint and its analytics; stop before continuing the turn.
let result = result.and_then(|_| sess.check_rollout_budget());
if let Err(err) = &result
&& !matches!(phase, CompactionPhase::PostTurn)
&& !matches!(
@@ -264,7 +267,7 @@ async fn run_compact_task_inner(
sess.send_event(&turn_context, event).await;
}
}
result.map(|_| ())
result
}
async fn run_compact_task_inner_impl(
@@ -899,8 +902,8 @@ async fn drain_to_completed(
.await??;
let mut output = Vec::new();
loop {
let maybe_event = stream.next_for_turn(cancellation).await;
let Some(event) = maybe_event else {
let Some(SamplingEvent { event, hook_output }) = stream.next_for_turn(cancellation).await
else {
return Err(CodexErr::Stream(
"stream closed before response.completed".into(),
));
@@ -945,7 +948,10 @@ async fn drain_to_completed(
.await;
sess.update_token_usage_info(
turn_context,
token_usage.as_ref(),
crate::client_common::SamplingUsage {
provider: token_usage,
charge: hook_output,
},
includes_reasoning,
)
.await?;
@@ -961,6 +967,7 @@ async fn drain_to_completed(
turn_context,
&turn_context.initial_settings,
incomplete,
hook_output,
)
.await;
sess.send_token_count_event(turn_context).await;