fix(codex): normalize LLM call logging to per-turn boundaries#841
Merged
Conversation
nhopeatall
approved these changes
Mar 14, 2026
Collaborator
nhopeatall
left a comment
There was a problem hiding this comment.
Summary
LGTM — Clean refactor that introduces a turn-scoped accumulator to normalize Codex LLM call logging from one-row-per-usage-event to one-row-per-completed-turn, with solid test coverage for the key scenarios.
The implementation correctly:
- Initializes the accumulator both at context creation (for streams that lack
turn.started) and onturn.started/thread.startedevents - Uses "last value wins" override semantics in
accumulateTurnUsage, soturn.completedaggregate totals correctly supersede intermediateresponse.completedper-response values - Persists exactly once per turn boundary and resets the accumulator afterward
- Removes the raw JSONL line from the stored payload in favor of a compact, structured turn summary (text + tools + usage)
- Preserves the existing
logLlmCallcontract withllmCallLogger.ts— no downstream schema or API changes needed
The 4 new tests cover the critical scenarios well: multi-turn sequencing, duplicate-usage prevention, compact payload shape, and the no-turn-completed edge case. CI passes cleanly.
🕵️ claude-code · claude-opus-4-6 · run details
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
storeLlmCallrow per usage-bearing event (e.g.response.completed), instead of one row per completed turn — resulting in a single giant aggregate call or duplicate rows per runCodexTurnAccumulator) that collects text, tool names, and usage across JSONL events within a turn; persistence fires exactly once onturn.completedagent_run_llm_callsand runs API/UI contract is preservedKey changes
src/backends/codex/index.tsCodexTurnAccumulatortype toCodexLineContextto track in-progress turn stateaccumulateTurnUsage(): merges intermediate usage events (e.g.response.completed) into the accumulator without persistingpersistTurnLlmCall(): called exactly once onturn.completed; writes a compact, turn-scoped payload (text summary + tool names + usage) rather than a raw event dumphandleStructuralEvent():turn.started/thread.startedreset the accumulator;turn.completedaccumulates any final usage then persistshandleParsedLine(): intermediate usage events now callaccumulateTurnUsage()instead oftrackUsage()(which is removed)turn.completedaggregate totals supersede intermediateresponse.completedper-response valuestests/unit/backends/codex.test.tsturn.started+turn.completedlifecycle events (reflecting correct behavior)storeLlmCallrows with stable sequential call numbersresponse.completedandturn.completedcarry usage — exactly 1 row storedturn,tools,usageand is < 2 KBturn.completedproduce no rowsTest plan
shared-llmCallLoggertests passpostComment,github) confirmed pre-existing on base branchCard: https://trello.com/c/ueUZYl4d/359-it-looks-like-for-codex-runs-we-store-only-one-gigantic-llm-call-in-our-agent-runs-llm-calls-lets-look-how-it-looks-with-other-e
🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com
🕵️ claude-code · claude-sonnet-4-6 · run details