Reduce AI token usage across planner/implementer/verifier/reviewer runs

Open
#40 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
15/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
javascript

Research direction

This is an umbrella design issue spanning handoff, memory injection and measurement in bundle/hooks/lib/handoff-enforcer.js, bundle/hooks/lib/token-tracker.js and bundle/hooks/orchestra-subagent-stop.js. Read token-tracker.js first to see how estimateTokens and AGENT_BUDGETS work, then the handoff builder. A newcomer should pick the linked sub-task #41 (per-stage token logging) instead; done for this issue means a recorded baseline, an agreed target, and the listed acceptance criteria met.

Written by the indexing model from the issue text.

Description

enhancement

Summary

Reduce AI token usage across planner → implementer → verifier → reviewer runs without losing quality. Today the budgets are 10k/30k/15k/8k (≈63k per cycle) but the only measurement is a rough estimate of output size, and there is no caching or summarisation layer for repeated reads.

Where tokens go today (grounded in code at v3.8.0)

  • Handoff: bundle/hooks/lib/handoff-enforcer.js — MAX_HANDOFF_CHARS = 2500, HANDOFF_RULES ("pass only goal + plan summary + failures"), buildPhaseHandoff includes last 3 phase summaries (120 chars each). bundle/hooks/orchestra-subagent-stop.js:~520-548 then adds evidence digest, commit-gate block, work-item context and six-file validation to the payload (buildHandoffEnforcement, spawnSubagent).
  • Memory injection: bundle/hooks/orchestra-prompt.js:279-280, orchestra-session-start.js:72, orchestra-memory.js:~14-25 each inject memory context independently.
  • Six-file context: bundle/hooks/lib/context-detector.js (buildContextInjection) tells every agent to read six context/*.md files in order.
  • Measurement: bundle/hooks/lib/token-tracker.js — estimateTokens = chars/4 (L70-73) on summary/result/output text only; reported usage (extractReportedUsage, cache-read tokens) used only if the host exposes it; budgets AGENT_BUDGETS / CYCLE_BUDGET (L14-21).

Proposed work

  1. Trim handoff context — send only what the next stage needs (e.g. plan bullets + failing check output), de-duplicate against what memory/six-file injection already provided.
  2. Cache repeated lookups — file reads, searches and memory recall within a run (and across runs when files are unchanged, keyed by path+mtime/hash). Candidate: a small per-run cache in the state dir (PCORE_STATE_DIR).
  3. Summarise long outputs before handoff — cap evidence/test output (e.g. keep failing assertions + last N lines), store the full log on disk and pass a path.
  4. Diffs instead of full files — verifier/reviewer receive git diff <diffBase> (the run already records state.diffBase in evidence-runner.js) plus file paths, not whole files.
  5. Per-stage measurement — record input and output tokens per stage with a baseline for a fixed set of sample tasks and a target (e.g. −25% total tokens per cycle at equal pass rate).
  6. Report — extend printTokenReport to show per-stage input/output, cache-hit rate and savings vs baseline.

Host notes

Hook-driven hosts (Cursor, Qoder, Claude Code) can measure at subagentStop; OpenCode and Codex are conductor-driven so measurement relies on what the host reports (document the fallback to estimates).

Acceptance criteria

  • Baseline numbers (tokens per stage and per cycle) for ≥3 sample tasks are recorded in docs/ or context/.
  • A target is agreed and documented; CI or a script reports against it.
  • Handoff payload size is measured and reduced; MAX_HANDOFF_CHARS enforced by a test.
  • Repeated file reads/searches/memory recall in one run hit a cache (tests for hit/miss/invalidations).
  • Long outputs are summarised with full log stored on disk and referenced.
  • Verifier/reviewer use diffs against diffBase.
  • No regression in the existing suites (test-adaptive-loop.js, handoff, evidence).

Beginner sub-task

Per-stage token logging is split out as its own good first issue (linked below).

Raised from the v3.8.0 audit (commit 22d2146).


Beginner sub-task: #41 (per-stage token logging).

Dominant language
JavaScript
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from P-Core-System/pcore-orchestra

All issues in P-Core-System/pcore-orchestra

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.