Skip to content

Optimize Coder/Lead orchestration LLM usage: measure prompt-section cost, stop cache-busting dynamic system prompts, and avoid redundant decider calls #768

Description

@serge-ivo

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

  1. Add usage instrumentation that records prompt-section byte/token estimates per kind (chat, coding, overseer, copilot) into trace/debug events, without storing prompt text.
  2. Split stable cached system prompt from dynamic per-turn context for agent-think and Pilot decisions.
  3. Add a deterministic short-circuit before runLoopDecide when the target agent reply/tool outcome already gives a terminal verdict.
  4. 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.

Activity

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

    P2: correctnessReal defect, no live harm today — inert fields, miscounts, missing guardsbackendBackend / Worker / API workcoderThe Coder wedge agent (#68) — Engine, Pilot, Co-pilot, Loop, OverseerenhancementNew feature or requestobservabilityA displayed value the code cannot produce, or that means something other than its label

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions