litellm/docs
jtstothard ef76139e9a
fix(copilot): correct X-Initiator header so only the first turn of multi-turn agentic workflows consumes a premium request
This PR fixes GitHub Copilot billing behavior for multi-turn agentic workflows by implementing
proper X-Initiator header classification. Previously, all turns consumed a premium request.
After this fix, only the first user-initiated turn consumes a premium request; follow-up
assistant/tool turns (agent-initiated) do not.

**Changes:**

1. **New X-Initiator header classification** (common_utils.py):
   - Added `determine_x_initiator()` helper that analyzes input messages/items
   - Returns "user" if messages are user-only (new conversation = premium request)
   - Returns "agent" if messages contain assistant/tool roles (continuation = free)

2. **Wired X-Initiator header into completion paths**:
   - Chat API: chat/transformation.py sets X-Initiator from validate_environment()
   - Responses API: responses/transformation.py sets X-Initiator from validate_environment()
   - Main completion path: main.py passes X-Initiator through extra_headers

3. **Optional conversation correlation header**:
   - Added `copilot_conversation_id` metadata parameter for session-scoped billing
   - Sets X-Conversation-Id header when provided (optional, for caller opt-in)
   - Verified via API testing that billing is governed by X-Initiator, not session correlation

4. **Comprehensive test coverage**:
   - Unit tests for X-Initiator classification logic (test_conversation_id.py)
   - Integration tests for Chat API (test_github_copilot_transformation.py)
   - Integration tests for Responses API (test_github_copilot_responses_transformation.py)
   - Billing integration tests (test_github_copilot_billing.py)

**Billing impact:**
- Multi-turn agentic workflows: 1 premium request instead of N (where N = number of turns)
- Single-turn conversations: No change (still 1 premium request)
- Tool calls: 1 premium request for the full user→tool→tool_response→assistant exchange

Fixes: #25276

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-21 16:06:50 +01:00
..
my-website fix(copilot): correct X-Initiator header so only the first turn of multi-turn agentic workflows consumes a premium request 2026-04-21 16:06:50 +01:00