Problem
Coder/Lead orchestration spends the owner's Anthropic BYOK key on cloud brain calls even when the local coding engine is subscription/unattributed. Live MCP usage reads on 2026-09-01 show the largest token/value number is engine work with payer unknown, not charged API-key spend; the charged API-key bucket is much smaller but is led by chat/coding orchestration calls.
30d snapshot from MCP usage_summary:
engine: $10,838.14 notional value, 739 calls, 84 sessions, $0 charged, mostly unattributed Claude Code/Opus work.
chat: $59.20 notional, $40.99 charged, 2,107 calls.
coding: $11.32 notional, $10.37 charged, 958 calls.
copilot: $1.92 notional, 85 calls.
overseer: $1.70 notional, 57 calls.
This is not #551. #551 is payer attribution for engine work. This ticket is about reducing the orchestration calls that actually spend BYOK/API-key tokens.
Suspected sinks
workers/api/src/agent-think.ts builds a large dynamic chat system prompt every turn: memory, tasks, directions, recent work, repo/session state, terminal tails, connector/tool descriptions.
workers/api/src/lib/user-ai.ts prompt-caches the whole system prompt as one block. Dynamic clock/repo/session/recent-work sections likely cause avoidable cache writes or misses.
workers/api/src/workflows/agent-loop.ts pays for the target agent chat, then workers/api/src/lib/loop-orchestrator.ts pays a second Sonnet call to decide continue/done/escalate every iteration.
workers/api/src/lib/coding-loop.ts Pilot decisions include the full step log plus a ~6k terminal tail every step, up to 30 steps, with PILOT_MAX_TOKENS = 2048.
workers/api/src/lib/coding-copilot.ts substantive Co-pilot questions can run up to three tool rounds plus a final answer, with timeline memory and terminal context.
MVP slice
- Add usage instrumentation that records prompt-section byte/token estimates per
kind (chat, coding, overseer, copilot) into trace/debug events, without storing prompt text.
- Split stable cached system prompt from dynamic per-turn context for
agent-think and Pilot decisions.
- Add a deterministic short-circuit before
runLoopDecide when the target agent reply/tool outcome already gives a terminal verdict.
- Cap Pilot
Steps so far to recent N plus a compact summary.
Acceptance
- Usage/trace can show top prompt sections by estimated input tokens for one run.
- Chat cache-write tokens drop on repeated turns with unchanged stable instructions.
- A loop iteration with an obvious terminal outcome avoids the second decider call.
- Existing safety, authority and grounding prompt claims remain covered by tests.
Non-goals
Provenance
Filed from a BA + live MCP usage investigation on 2026-09-01. MCP tools used: usage_summary, keys_status. Anthropic is the currently active BYOK key; OpenAI has not been used since 2026-08-12.
Problem
Coder/Lead orchestration spends the owner's Anthropic BYOK key on cloud brain calls even when the local coding engine is subscription/unattributed. Live MCP usage reads on 2026-09-01 show the largest token/value number is engine work with payer unknown, not charged API-key spend; the charged API-key bucket is much smaller but is led by chat/coding orchestration calls.
30d snapshot from MCP
usage_summary:engine: $10,838.14 notional value, 739 calls, 84 sessions, $0 charged, mostly unattributed Claude Code/Opus work.chat: $59.20 notional, $40.99 charged, 2,107 calls.coding: $11.32 notional, $10.37 charged, 958 calls.copilot: $1.92 notional, 85 calls.overseer: $1.70 notional, 57 calls.This is not #551. #551 is payer attribution for engine work. This ticket is about reducing the orchestration calls that actually spend BYOK/API-key tokens.
Suspected sinks
workers/api/src/agent-think.tsbuilds a large dynamic chat system prompt every turn: memory, tasks, directions, recent work, repo/session state, terminal tails, connector/tool descriptions.workers/api/src/lib/user-ai.tsprompt-caches the whole system prompt as one block. Dynamic clock/repo/session/recent-work sections likely cause avoidable cache writes or misses.workers/api/src/workflows/agent-loop.tspays for the target agent chat, thenworkers/api/src/lib/loop-orchestrator.tspays a second Sonnet call to decide continue/done/escalate every iteration.workers/api/src/lib/coding-loop.tsPilot decisions include the full step log plus a ~6k terminal tail every step, up to 30 steps, withPILOT_MAX_TOKENS = 2048.workers/api/src/lib/coding-copilot.tssubstantive Co-pilot questions can run up to three tool rounds plus a final answer, with timeline memory and terminal context.MVP slice
kind(chat,coding,overseer,copilot) into trace/debug events, without storing prompt text.agent-thinkand Pilot decisions.runLoopDecidewhen the target agent reply/tool outcome already gives a terminal verdict.Steps so farto recent N plus a compact summary.Acceptance
Non-goals
machine-logintosubscription; that is 99.62% of the account's AI value is "Payer not established" — one stored claude-code token would resolve it, and nothing on any surface says so #551 and would be false attribution.Provenance
Filed from a BA + live MCP usage investigation on 2026-09-01. MCP tools used:
usage_summary,keys_status. Anthropic is the currently active BYOK key; OpenAI has not been used since 2026-08-12.