Skip to content

DRU-1144 -- Count the tokens and cost of a Codex call's subagents - #1171

Open
p-r-a-v-i-n wants to merge 1 commit into
czpython:mainfrom
p-r-a-v-i-n:dru-1144-codex-subagent-usage
Open

p-r-a-v-i-n wants to merge 1 commit into
czpython:mainfrom
p-r-a-v-i-n:dru-1144-codex-subagent-usage

Conversation

@p-r-a-v-i-n

Copy link
Copy Markdown
Collaborator

A Codex call now records the tokens and cost of the subagents it spawns, together with its own. Before, Druks read only the main thread: turn.completed on stdout carries only the main thread's totals, and each subagent keeps its totals in its own session file under ~/.codex/sessions. With a subagent running, more than one session file is newer than the marker, so the wrapper also skipped session.jsonl, and a failed call with subagents recorded nothing.

  • The wrapper treats a session file whose first line has a thread_spawn source as a subagent's file. For each one, it writes the first line, the last turn_context and the last token_count to a new subagents.jsonl artifact. It picks session.jsonl from the other files as before.
  • read_codex_subagent_costs keeps the subagents whose parent_thread_id is the main thread id from the thread.started event on stdout, so another call's files on the same sandbox are not counted. It prices each subagent at the model on its turn_context, because spawn_agent can pick a different model.
  • parse adds each subagent's cost and tokens to the main thread's totals. The model and rate fields in the cost metadata still describe the main thread.
  • A subagent cannot spawn its own subagents with Codex's default agents.max_depth of 1, so only direct children are counted.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant