feat(tracing): add trace_scope to group a Codex session into one trace#27
Open
TropicalDog17 wants to merge 1 commit into
Open
feat(tracing): add trace_scope to group a Codex session into one trace#27TropicalDog17 wants to merge 1 commit into
TropicalDog17 wants to merge 1 commit into
Conversation
Upstream emits one Langfuse trace per Codex turn, linked only by session id, so an active session floods the traces list with near-identical "Codex Turn" rows. trace_scope="session" pins every turn of a thread to one deterministic trace id derived from the thread id, so the session becomes a single "Codex Session" trace whose top-level spans are its turns (generations, tool calls and subagents still nested underneath). Defaults to "turn", so existing behaviour is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
Every Codex turn becomes its own Langfuse trace, linked to its siblings only through the session id. A working session therefore fills the traces list with a long run of near-identical
Codex Turnrows, and the unit people actually reason about — the session — has no single trace to open.Change
Adds a
trace_scopeconfig key (LANGFUSE_CODEX_TRACE_SCOPE):"turn"(default) — today's behaviour, unchanged."session"— every turn of a thread is pinned to one deterministic trace id derived from the Codex thread id, so the session is a singleCodex Sessiontrace whose top-level spans are its turns. Generations, tool calls, and subagents stay nested underneath exactly as before; cost, tokens, and latency aggregate over the session.The id is
hex(sha256("codex-session:<threadId>")).slice(0, 32), so each Stop-hook invocation appends to the same trace. It composes withtrace_seed, which becomeshex(sha256("<seed>:<threadId>")).slice(0, 32)— still precomputable by an external harness.This reuses the existing
SEED_PARENT_SPAN_IDplaceholder-parent mechanism, so no new tracing concepts are introduced.Notes / trade-offs (documented in the README)
Testing
trace.test.ts: all turns of a thread share one session-derived trace id and remain top-level sibling spans; distinct threads get distinct traces;trace_scopecomposes withtrace_seed.pnpm run lint(prettier,tsc --noEmit, and thelint:distbundle-matches-source check).Codex Sessiontrace containing 29 turn spans with their generations and tool calls nested underneath.🤖 Generated with Claude Code