[PR #12932] feat: send x-session-id and x-parent-session-id headers in LLM API requests #14442

Open
opened 2026-02-16 18:19:14 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12932

State: open
Merged: No


Summary

Forward opencode session identifiers as HTTP headers on all outgoing LLM API requests:

  • x-session-id: Always present — identifies the current opencode session
  • x-parent-session-id: Present only for sub-agent (task tool) sessions — identifies the parent session that spawned it

This enables any OpenAI-compatible backend to correlate requests belonging to the same session and reconstruct the full agent workflow tree when sub-agents are spawned.

Motivation

When running opencode against a local OpenAI-compatible server, there is currently no way for the server to determine which session a request belongs to, or whether it comes from a sub-agent. All requests arrive as independent /v1/chat/completions calls with no identifying metadata.

With these headers, a server can trivially build a tree view:

▼ ses_abc123 - "Help me refactor the auth module"
    ├─ ses_def456 - [task] glob search "src/**/*.ts"
    ├─ ses_ghi789 - [task] grep "authenticate"
    └─ ses_jkl012 - [task] reading 3 files

Changes

  • packages/opencode/src/session/llm.ts: Add optional parentSessionID to StreamInput type; send x-session-id and x-parent-session-id headers on all LLM requests
  • packages/opencode/src/session/prompt.ts: Pass session.parentID through as parentSessionID when building the stream input

Design

  • Non-breaking: parentSessionID is optional — existing callers (summary, title agents) that don't pass it simply omit the parent header
  • Additive headers: Providers that don't use these headers will ignore them
  • Minimal diff: 4 lines added across 2 files
  • Leverages existing data: session.parentID is already set by the task tool when creating sub-agent sessions (see tool/task.ts:73)

Related

Test plan

  • Verify x-session-id header is present on all outgoing LLM requests
  • Verify x-parent-session-id is present when a task tool sub-agent makes requests
  • Verify x-parent-session-id is absent for top-level session requests
  • Verify existing functionality (summary, title agents) continues working
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12932 **State:** open **Merged:** No --- ## Summary Forward opencode session identifiers as HTTP headers on all outgoing LLM API requests: - **`x-session-id`**: Always present — identifies the current opencode session - **`x-parent-session-id`**: Present only for sub-agent (task tool) sessions — identifies the parent session that spawned it This enables any OpenAI-compatible backend to correlate requests belonging to the same session and reconstruct the full agent workflow tree when sub-agents are spawned. ## Motivation When running opencode against a local OpenAI-compatible server, there is currently no way for the server to determine which session a request belongs to, or whether it comes from a sub-agent. All requests arrive as independent `/v1/chat/completions` calls with no identifying metadata. With these headers, a server can trivially build a tree view: ``` ▼ ses_abc123 - "Help me refactor the auth module" ├─ ses_def456 - [task] glob search "src/**/*.ts" ├─ ses_ghi789 - [task] grep "authenticate" └─ ses_jkl012 - [task] reading 3 files ``` ## Changes - **`packages/opencode/src/session/llm.ts`**: Add optional `parentSessionID` to `StreamInput` type; send `x-session-id` and `x-parent-session-id` headers on all LLM requests - **`packages/opencode/src/session/prompt.ts`**: Pass `session.parentID` through as `parentSessionID` when building the stream input ## Design - **Non-breaking**: `parentSessionID` is optional — existing callers (summary, title agents) that don't pass it simply omit the parent header - **Additive headers**: Providers that don't use these headers will ignore them - **Minimal diff**: 4 lines added across 2 files - **Leverages existing data**: `session.parentID` is already set by the task tool when creating sub-agent sessions (see `tool/task.ts:73`) ## Related - Closes #12930 - Related to #219 (Langfuse tracing), #6142 (sessionID in hooks), #9292 (session context in env vars), #11466 (Mistral x-affinity header) ## Test plan - [ ] Verify `x-session-id` header is present on all outgoing LLM requests - [ ] Verify `x-parent-session-id` is present when a task tool sub-agent makes requests - [ ] Verify `x-parent-session-id` is absent for top-level session requests - [ ] Verify existing functionality (summary, title agents) continues working
yindo added the pull-request label 2026-02-16 18:19:14 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14442