Repository navigation
Description
Problem or Motivation
The gateway cannot group the model calls of one agent run. No project caller sends x-litellm-trace-id or x-litellm-session-id. gateway_caller_headers (src/devops_cli/ai/client/network.py:64-80) sends only x-litellm-end-user-id. Thus the spend of one stage round, one refine investigation or one implement run is not visible without a log join. No data shows how many runs make too many calls.
The A2A study of 2026-11-13 found that each gateway feature for agents needs this id. The LiteLLM per-run caps (max_iterations, max_budget_per_session) count by session. A2A spend attribution also uses X-LiteLLM-Trace-Id.
Proposed Solution
- Add a context variable for the run id.
src/devops_cli/roadmap/agent_run.pyand each row that calls a model set it at the start of a run. - The run id has the form
<job>-<item>-<attempt>-<8 hex>. It holds no email address and no repository owner. It has at most 64 characters. gateway_caller_headersaddsx-litellm-trace-idwhen the variable has a value. The request hook innetwork.pyadds the header for both client stacks. If the pydantic-ai bridge does not use this hook, add the hook to the bridge.- A call outside a run sends no such header. Workstation calls do not change.
- Put the header name in
src/devops_cli/config/constants.pynext toCONST_AI_GATEWAY_SERVED_BY_HEADER. - Add one sentence about the run id to the gateway section of
k8s/README.md.
Out of scope: a cap on the run. The data of this item decides the follow-up item for the per-run gateway cap.
Acceptance Criteria
- A test with an httpx mock transport proves that two requests of one run carry the same
x-litellm-trace-id, and that two runs carry different values. The test covers LLMClient andbuild_agent. - A test proves that a call outside a run sends no
x-litellm-trace-id. - A test proves that the run id has at most 64 characters and holds no
@. - The pull request shows one live gateway spend row of a stage round with the run id in its session field. The output shows no key value.
-
k8s/README.mdnames the header and its form.
Existing Tool Considered
LiteLLM reads x-litellm-trace-id and x-litellm-session-id and maps them to metadata.session_id (litellm/proxy/litellm_pre_call_utils.py:83, checked in the 1.103.0 source). Confirm in the Feasibility section that the spend log of v1.103.11 keeps this session id. The W3C traceparent header already goes to the gateway, but LiteLLM does not use it as the session key. So the project builds no store and sends one header. Build cost (rough estimate): 0.3 M tokens and 3 hours. Keep: none. Fix: 0.05 M tokens for each upstream rename of the header.
Related
#3066 (day and repository budgets), #3685 (Service gateway key), #3578 (caller with no end-user header), #4716, #5427.
Alternatives check of 2026-11-28: in-use.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-28, maintained.
Reason: Searches returned little, but the facts of BerriAI/litellm, the gateway the project already runs, show it groups calls by session id from the trace header and records per-session spend; only the client-side header send is missing.
Covers:
- Groups each run's calls under one session id in spend logs
- Maps the trace and session headers to session metadata
- Per-session budget and iteration caps already counted by session
- No new store, dashboard or spend table to build
Lacks:
- No client-side run context or automatic run id
- Callers must send the header on every client stack
- No proof yet that v1.103.11 spend rows keep the session id
- Bridge request hook must be wired by the project
Proposed design
Problem statement
The gateway cannot group the model calls of one agent run. No caller sends a run id header. The change adds a run context and sends x-litellm-trace-id on each gateway call of a run. It documents the header. A live spend-row check is deferred.
Acceptance criteria
Requirement: Run context for each agent run
The shared agent-run helper SHALL set a run id in a context variable for the duration of one run and SHALL reset it after the run.
Scenario: Run context set and reset
- WHEN a test enters the agent-run context for job
stage, item5437and attempt1, then leaves it - THEN the context holds a non-empty run id inside the block and holds no value after the block.
Requirement: Trace header from the run context
The gateway caller headers SHALL include x-litellm-trace-id with the run id when the context has a value.
Scenario: Header mapping with a run id
- WHEN a test sets the run context to
refine-5437-1-0123abcdand callsgateway_caller_headers() - THEN the mapping has
x-litellm-trace-idequal torefine-5437-1-0123abcd.
Requirement: Same run id for each request of a run
Each gateway request of one agent run SHALL carry the same x-litellm-trace-id, and a request of another run SHALL carry a different value.
Scenario: LLMClient and build_agent requests in two runs
- WHEN a test uses an httpx mock transport with
LLMClientandbuild_agentfor two runs - THEN both requests of the first run have the same
x-litellm-trace-id, and the request of the second run has a different value.
Requirement: No trace header outside a run
A gateway call outside an agent run MUST send no x-litellm-trace-id header.
Scenario: Request with no run context
- WHEN a test makes a gateway request with
LLMClientwhile the run context is empty - THEN the request has no
x-litellm-trace-idheader.
Requirement: Run id form
The run id SHALL have the form <job>-<item>-<attempt>-<8 hex>, SHALL have at most 64 characters, and SHALL hold no @.
Scenario: Run id from a job name with an email-like part
- WHEN a test builds a run id for job
[email protected], item5437and attempt2 - THEN the id matches
^[a-z0-9][a-z0-9_-]*-[0-9]+-[0-9]+-[0-9a-f]{8}$, has at most 64 characters, and holds no@.
Requirement: Gateway trace header constant
The header name MUST be the constant CONST_AI_GATEWAY_TRACE_HEADER in src/devops_cli/config/constants.py.
Scenario: Constant value
- WHEN a test imports
CONST_AI_GATEWAY_TRACE_HEADER - THEN the constant equals
x-litellm-trace-id.
Requirement: Gateway README names the run id header
The gateway section of k8s/README.md SHALL name x-litellm-trace-id and the run id form.
Scenario: Docs CI after the README change
- WHEN the docs CI check runs with
uv run devops ci --check --only docs - THEN the command passes with the updated
k8s/README.md.
Existing Tool Considered
BerriAI/litellm (https://github.com/BerriAI/litellm), verdict in-use: the repository already uses this tool. The alternatives check in the issue body records what the tool covers and lacks. The proposal adds the client-side run context and header path, the tests, and the README sentence.
Key questions
- Where should the run-id context variable live?: Define it in a new
src/devops_cli/ai/client/run_context.pymodule.network.pyimports it, and the roadmap helper sets and resets it. (best practice) [sources: src/devops_cli/ai/client/network.py:65, src/devops_cli/roadmap/agent_run.py, src/devops_cli/ai/client/factory.py:252]- Chosen: New AI client run-context module. The AI client can import the variable without importing roadmap code.
- Rejected: Define it in roadmap/agent_run.py. The AI client would import a roadmap module and create a cycle.
- Rejected: Pass the run id through each call signature. It changes many call sites and the request hook cannot receive the id.
- Which header should carry the run id to the gateway?: Use
x-litellm-trace-idwith the run id. LiteLLM maps it tometadata.session_id. (best practice) [sources: src/devops_cli/ai/client/network.py:65, src/devops_cli/config/constants.py:3108]- Chosen: x-litellm-trace-id. The issue and the A2A study name it, and LiteLLM maps it to the session id.
- Rejected: x-litellm-session-id. No caller uses it, and the issue names the trace id header.
- Rejected: W3C traceparent. LiteLLM does not use it as the session key.
- How does the pydantic-ai bridge send the run id header?: Register the
name_gateway_callerrequest hook on the httpx client thatbuild_agentcreates. The hook adds the trace id from the context, so both client stacks send it. (best practice) [sources: src/devops_cli/ai/client/network.py:65, src/devops_cli/ai/client/factory.py:252]- Chosen: Shared request hook on the bridge client. It keeps one header path for LLMClient and the bridge.
- Rejected: Only add the header in gateway_caller_headers. It does not prove the bridge sends the header if the bridge bypasses the hook.
- Rejected: Build a separate bridge header path. It duplicates logic and can drift.
- Does LiteLLM keep the session id in its spend log?: The alternatives check records that LiteLLM maps
x-litellm-trace-idtometadata.session_idand records per-session spend in the spend log. It does not name v1.103.11. The live row check is deferred to a new issue. [sources: src/devops_cli/ai/client/network.py:65]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Activity
dan-petty commentedon Nov 13, 2026
Both items modify gateway_caller_headers in network.py, but #3578 handles a missing end-user header while this item adds a new trace-id header for run grouping.devops-cli-service commentedon Nov 17, 2026
Alternatives check: in-use
Reason: Searches found no tool other than the LiteLLM gateway the project already calls, and its README confirms it is a self-hosted AI gateway with spend tracking, virtual keys and A2A support. Per the issue's own reading of the 1.103.0 source, LiteLLM maps x-litellm-trace-id / x-litellm-session-id to metadata.session_id, records spend per session and enforces the per-session caps, so it covers the gateway side; the client-side context variable, header sending in network.py and the pydantic-ai bridge, the tests and the README sentence stay the project's own work.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm)
Covers:
Lacks:
Tools considered:
Searches run:
Sources:
Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.
devops-cli-service commentedon Nov 17, 2026
Status set to Ready at 0581ec5 (see proposed design in issue body).
Moved to new items: #6232.
feat(ai): send per-run trace header to gateway (#5437)
chore(main): release 0.248.0 (#7763)
devops-cli-service commentedon Nov 29, 2026
Released in v0.248.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.248.0
devops-cli-service commentedon Nov 29, 2026
Live since 2026-11-29 16:50 UTC