Meant to be used by agents from any harness: delegate execution to a cheaper agent to save tokens, have a weaker agent execute while a stronger peer validates its plans, or let two agents challenge, verify, and cross-check each other's ideas.
Peer Agent is a stateful model harness. A host agent sends a prompt (plus an optional
conversation_id); the service holds history server-side and runs it on Anthropic,
hosted OpenAI, or local OpenAI-dialect models with server-side read/bash/edit tools,
prompt caching, live tool logging, and automatic compaction.
-
cp .env.example .envand put your key in.env. -
Start the service from this directory:
python -m service.server --port 8471 --model anthropic/claude-opus-5-5. -
Start any coding agent from your project directory (not here).
-
Ask your agent to read the
<abs-path-to-this-repo>/interface/SKILL.md. -
Now you can ask your agent to talk to peer agent, e.g. "validate your plan with peer agent".
cp .env.example .env # then put your key in .env (git-ignored)
python -m service.server --port 8471 # default backend: anthropic
python -m service.server --model stub --port 8471 # offline smoke testDefault is anthropic/claude-haiku-5-5 (cheapest, so careless runs stay cheap).
Pick another with --model provider/model (model names as of Oct 2026):
python -m service.server --model anthropic/claude-haiku-5-5 --port 8471 # cheap testing
python -m service.server --model anthropic/claude-sonnet-5-5 --port 8471 # balanced
python -m service.server --model anthropic/claude-opus-5-5 --port 8471 # strongest, priciestHosted OpenAI (model names as of Oct 2026):
python -m service.server --model openai/gpt-4.1-nano --port 8471 # cheap testing
python -m service.server --model openai/gpt-5.4 --port 8471 # frontierLocal OpenAI-dialect endpoint (llama.cpp, vLLM, Ollama):
python -m service.server --model local --base-url http://localhost:9931/v1 \
--model-context-window 150000 --compact-in-context --port 8472
# multi-model servers (Ollama, vLLM): select with an id, e.g.
python -m service.server --model local/qwen3:32b --base-url http://localhost:11434/v1 \
--model-context-window 131072 --compact-in-context --port 8472--compact-in-context compacts via a transient in-context turn (warm KV
cache) with fallback to a separate summarize call — see
docs/compaction.md. Omit it for the default new-thread path.
Providers: anthropic, openai (hosted), local (any OpenAI-dialect
endpoint — needs --base-url, e.g. http://localhost:9931/v1), stub.
Single-model servers ignore the model field, so --model local alone
suffices (an alias like local/qwen is passed through and logged).
API keys come from provider env vars (ANTHROPIC_API_KEY,
OPENAI_API_KEY, LOCAL_MODEL_API_KEY); --api-key-env NAME overrides
with another variable. Providers without a metadata API take
--model-context-window for the true input budget.
The active model is recorded on every response line in logs/, so a
mid-thread model switch stays attributable.
Every request marks ephemeral cache breakpoints on the system prompt, the
last tool definition, and the history tail, so repeated history is served
from cache (5-min TTL) instead of rebilled. Breakpoints attach by default and
suspend only after n = _CACHE_IDLE_DISABLE_AFTER (in
service/backends/anthropic_backend.py) consecutive gaps over 5 minutes.
See docs/prompt-caching.md for pricing, the breakeven derivation, and the
keepalive-vs-1h analysis.
Watch cached_prompt_tokens vs cache_creation_input_tokens on response
lines in logs/.
All state is files under the repo, per conversation id:
conversations/<id>.json— live message history (what the next turn sends).conversations/<id>.meta.json— system prompt plus the last billed input size driving auto-compaction.conversations/backups/<id>/— pre-compaction history snapshots (newest 5).logs/<id>.jsonl— append-only event stream, one JSON object per line:prompt,tool_call/tool_result,response,compaction, andsystem_promptevents. This is the audit trail: history can be compacted, logs never are. Pass--verboseto also print each line to stderr live.
usage on response lines uses the same names on every backend:
| field | meaning |
|---|---|
total_input_tokens |
everything the model received as input: fresh + cache-read + cache-write. The window-relevant size. |
fresh_input_tokens |
input computed fresh this call (excludes cache). |
cached_prompt_tokens |
input served from cache. |
cache_creation_input_tokens |
input written to cache (where the provider reports it). |
completion_tokens |
output tokens. |
Invariant: fresh + cached + written = total_input_tokens (single-call
turns; multi-call tool turns accumulate cache counters, so divide by
tool_turns + 1 for a per-call estimate). total_tokens is removed; old
log lines containing it still parse.
python client/chat.py "explain this repo" --port 8471
python client/chat.py "go on" --conversation-id <id> --port 8471
python client/chat.py "long answer" --stream --port 8471Raw HTTP:
curl -s localhost:8471/chat -H 'content-type: application/json' \
-d '{"prompt":"hi","cwd":"/abs/host/workdir"}'
# {"text":"...","conversation_id":"..."}Long threads auto-compact: when the last billed input reaches
--compact-trigger-tokens (exact total_input_tokens, no reserve or margin),
old turns are summarized and replaced, keeping the last
--compact-keep-messages (default 20). Without a trigger, the fallback
window formula applies: est_history + max_output_tokens + effective_margin exceeding the model's input budget (read from the Models
API at startup, 1M for current Claude 5.5 models), where est_history is
a chars/4 heuristic and the effective margin is
max(--compact-margin-tokens, 5% of window). Full history stays in logs/;
compactions are logged as compaction events. When the trigger is set but
no billed usage exists yet (first turn, or backends that report none), the
server warns on stderr and falls back to the window formula for that turn.
Per-turn override:
{"compact": true/false}. Manual, no model turn: POST /compact {"conversation_id": "..."} (CLI: python client/chat.py "/compact" --conversation-id <id> --port 8471). On context-overflow the turn
retries once after compaction instead of failing. Slow-prefill local
models should pass --compact-in-context (warm-cache summaries, silent
fallback). See docs/compaction.md for paths, trade-offs, and all knobs.
Server knobs:
--compact-trigger-tokens (e.g. 300000 to compact a large model early),
--compact-model (e.g. haiku for cheap summaries), --compact-summary-max-tokens,
--max-output-tokens (per-call output budget, also the window-formula reserve),
--compact-in-context.
See interface/SKILL.md for the plain-HTTP instructions any agent can follow.
Stdio wrapper over the same HTTP service:
python interface/mcp_server.py --port 8471opencode project config (opencode.jsonc). Use absolute paths — the wrapper runs from the host agent's project directory, not from here:
This exposes a peer_chat(prompt, conversation_id?, cwd?) tool.
See docs/host-instruction-examples.md for copy-pasteable coordination prompts: a capable host working with a stronger peer reviewer, or a strong host delegating narrow subtasks to a weaker peer executor.
- service/server.py — FastAPI app:
POST /chat,POST /chat/stream,POST /compact,GET /health - service/compaction.py — margin trigger, tool-pair-safe split, summary helpers
- service/in_context_compaction.py — warm-cache path (
--compact-in-context) - service/backends/ — base.py interface, anthropic_backend.py, stub_backend.py
- service/tools/ — registry.py (open for new tools), bash_tool.py, read_tool.py, edit_tool.py
- service/store.py, service/event_log.py —
conversations/*.json,logs/*.jsonl - client/chat.py, interface/mcp_server.py
Apache-2.0, Copyright 2026 Arnab Bose. See LICENSE.
{"mcp": {"servers": {"peer-agent": { "type": "local", "command": ["/abs/path/to/venv/python", "/abs/path/to/peer-agent/interface/mcp_server.py", "--port", "8471"], }}}}