Skip to content

feat(ai): send one run id to the gateway for each agent run #5437

Done

Description

@dan-petty

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

  1. Add a context variable for the run id. src/devops_cli/roadmap/agent_run.py and each row that calls a model set it at the start of a run.
  2. 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.
  3. gateway_caller_headers adds x-litellm-trace-id when the variable has a value. The request hook in network.py adds the header for both client stacks. If the pydantic-ai bridge does not use this hook, add the hook to the bridge.
  4. A call outside a run sends no such header. Workstation calls do not change.
  5. Put the header name in src/devops_cli/config/constants.py next to CONST_AI_GATEWAY_SERVED_BY_HEADER.
  6. 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 and build_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.md names 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, item 5437 and attempt 1, 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-0123abcd and calls gateway_caller_headers()
  • THEN the mapping has x-litellm-trace-id equal to refine-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 LLMClient and build_agent for 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 LLMClient while the run context is empty
  • THEN the request has no x-litellm-trace-id header.

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], item 5437 and attempt 2
  • 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.py module. network.py imports 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-id with the run id. LiteLLM maps it to metadata.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_caller request hook on the httpx client that build_agent creates. 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-id to metadata.session_id and 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

  1. added
    source/agent
    type/feature
    scope/ai
    status/new
    value/medium
    effort/medium
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    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)

    • License: NOASSERTION
    • Last activity: 2026-11-17
    • Maintained: yes

    Covers:

    • Gateway side of the need: reads x-litellm-trace-id / x-litellm-session-id and maps them to metadata.session_id
    • Groups the model calls of one run and records per-session spend in the spend log
    • Enforces per-session caps (max_iterations, max_budget_per_session) that count by that session id
    • Already accepts A2A agent traffic, so the A2A spend attribution has a session key to use
    • No new store or grouping service is needed: one header is enough

    Lacks:

    • No client-side run id: the context variable, setting it in agent_run.py and in each row that calls a model, is project code
    • Does not send the header itself; gateway_caller_headers and the request hook in network.py must add it, and the pydantic-ai bridge may need the hook
    • No httpx mock-transport tests for same-run / different-run / no-run headers, and no 64-character and no-@ check on the run id
    • No documentation change for the gateway section of k8s/README.md
    • No per-run cap; the issue leaves that to a follow-up item

    Tools considered:

    Searches run:

    • litellm max_budget_per_session max_iterations per session spend, results: 5
    • LiteLLM x-litellm-trace-id session_id metadata spend logs, results: 5
    • "x-litellm-trace-id" session_id litellm docs, results: 5
    • litellm proxy spend logs session_id trace id grouping requests, results: 5
    • BerriAI litellm github, results: 5
    • litellm proxy documentation session tracking, results: 5
    • docs.litellm.ai proxy spend tracking session, results: 5
    • LiteLLM session tracking trace id header, results: 5
    • litellm, results: 5
    • litellm proxy "session_id" spend logs metadata docs.litellm.ai, results: 5

    Sources:

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  5. added
    status/ready
    and removed
    status/new
    on Nov 17, 2026
  6. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 0581ec5 (see proposed design in issue body).

    Moved to new items: #6232.

  7. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 29, 2026
  8. removed
    status/in-review
    on Nov 29, 2026
  9. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 16:50 UTC

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/aisource/agenttype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions