A pi agent with a long, stable system prompt runs three tool-calling turns through Mycel. Each request shows how many prompt tokens came from cache. A probe then sends the same prompt with and without pi's Mycel compat.cacheControlFormat: "anthropic" to show that the breakpoints are what make caching happen.
Run it
pnpm exec tsx examples/03-mycel-prompt-caching/run.ts # offline, mock Mycel
pnpm exec tsx examples/03-mycel-prompt-caching/run.ts --smoke # offline, PASS/FAIL lines, exit 0/1
MYCEL_URL=https://mycel.thefocus.ai MYCEL_API_KEY=... \
pnpm exec tsx examples/03-mycel-prompt-caching/run.ts --live --smoke [--model mycel:<id>]
Offline it pins lab/balanced, the model Lab 02 selects from the mock catalog. Live it defaults to mycel:deepseek/deepseek-v4.1-flash, the cheapest model that passed every live check.
Walkthrough
- AGENT: three turns through
runPiTurnwith a ~5.5k-token handbook prompt and alookup_sectiontool. The table shows the breakpoints sent, fresh input, cache read, cache write and hit % per request. - Offline, request 1 writes the prefix and every later request reads it (99%).
- CACHE: three identical requests without the compat, then three with it. Offline, only the requests with breakpoints read from cache.
- Live, pin
--model mycel:anthropic/claude-opus-5.5: it caches only with breakpoints. GLM caches some of the time. - Remove
cacheControlFormat: "anthropic"from the Mycel compat inpackages/pi/src/models.tsand rerun--smoke: the breakpoint and cache-read checks fail.
Files
run.ts: the agent run, the cache probe and the checks.mock-mycel.ts: offline Mycel that caches only atcache_controlbreakpoints, like hosted Mycel's upstream.PROMPT.md: a prompt to rebuild this folder.FINDINGS.md: what we learned and the latest smoke output.
Prompt
Build a standalone lab in examples/03-mycel-prompt-caching/ of the umwelten repo. It tests one thing: a pi agent on Mycel sends cache_control breakpoints, so later turns read its long system prompt from the upstream cache. Read first: - packages/pi/src/models.ts (resolvePiModel, runPiTurn, tool, ModelSpec, and the "mycel" provider's compat.cacheControlFormat: "anthropic") - packages/mycel/src/buyer/handler.ts (metering; ADR 0017: Mycel ignores cached_tokens when billing) - examples/tsconfig.json Files to write (do not import from other example folders; duplicate code if needed): - mock-mycel.ts: a node:http server on 127.0.0.1:0 with a bearer key. GET /v1/models returns a small catalog with pricing. POST /v1/chat/completions streams OpenAI-style SSE with tool calls from a scripted model (calls lookup_section for each "section N", quotes tool results, combines codewords when asked). Emulate an OpenRouter-style upstream cache: segments are [tools, ...messages]; a prefix is cached only at a cache_control breakpoint (last tool, or a message carrying one) and only if it is at least 1024 tokens (chars/4); compare prefixes with cache_control stripped and one-part text arrays folded to strings; a later request reads the longest cached prefix up to each breakpoint. Report usage.prompt_tokens_details.cached_tokens and cache_write_tokens. Record model, bearer and X-Mycel-End-User for each request. - run.ts: offline mock by default (pin the mock's "lab/balanced"); --live uses MYCEL_URL (default https://mycel.thefocus.ai) and MYCEL_API_KEY with default model mycel:deepseek/deepseek-v4.1-flash; --model mycel:<id> pins another. Read the pinned model's price from GET /v1/models. Set process.env.MYCEL_API_KEY and MYCEL_END_USER before calling pi. Wrap globalThis.fetch to record each chat request's model and number of cache_control keys. AGENT: a ~5.5k-token handbook system prompt with a random nonce, a lookup_section tool, and three runPiTurn turns (section 7, section 23, combine). Print a per-request table from the assistant messages' usage (breakpoints, fresh in, cache read, cache write, out, hit %) and the retail charge. CACHE: resolvePiModel, then 3 identical models.stream calls with compat.cacheControlFormat removed and 3 with it, each with a fresh system prompt. Checks: each turn called the expected tool and quotes the codewords; every agent request carried at least 2 breakpoints and used the pinned model; a later request read the cached prefix; no breakpoints without the compat; breakpoints with it; the probe read from cache with breakpoints. Offline only: the first request writes and reads nothing; the probe reads only with breakpoints; the mock saw the bearer and X-Mycel-End-User on every request; the fetch tap saw every request the mock served. --smoke prints one PASS/FAIL line per check and exits 0 only if all pass. - README.md (YAML frontmatter: title, track, status, tests, needs, run, smoke, deck), PROMPT.md, FINDINGS.md. Strict TypeScript, no `any`. Verify with pnpm exec tsc --noEmit -p examples/tsconfig.json, pnpm exec eslint --no-ignore "examples/03-*/**/*.ts", and run.ts --smoke offline and --live --smoke. Live caching is best-effort for non-Anthropic upstreams; report failures, do not weaken checks.
--live opt-in
What we learned
- pi did not cache through Mycel. pi-ai only adds
cache_controlbreakpoints for provideropenrouterwithanthropic/models. Mycel relays to an OpenRouter-style upstream, so Opus never cached. Fix:@umwelten/pisetscompat.cacheControlFormat: "anthropic"for themycelprovider (bothregisterMycelProviderOnand the ad-hoc target). Live Opus: 0 → 6,612 cached tokens on repeat requests. - Providers differ. With breakpoints, live GLM-5.3-Flash read most of its ~4,600-token prompt from cache on some later agent turns, but not reliably. Its isolated three-call probe is inconsistent: some runs show 0 · 0 · 0 with breakpoints and a 4,096 hit without. DeepSeek hits with or without breakpoints. Upstream routing is not sticky, so cache reads are best-effort for non-Anthropic models.
- Only some upstreams report cache writes. The mock and Opus report
cache_writeon the first request. GLM reports 0 on every request, so the "first request writes" check runs offline only. - Caching does not lower the Mycel bill today. Mycel meters prompt tokens itself and ignores
cached_tokens(ADR 0017), so a cache read is charged at full prompt price. Caching is a latency win for the buyer and a margin win for the Exchange. @umwelten/pidefaultsMYCEL_URLtolocalhost:7438. Set it explicitly for hosted Mycel.
- Live default is DeepSeek V4.1 Flash. It passed 11/11 live on two runs, with 59–79% of prompt tokens read from cache, for about $0.004 a run. It caches with or without breakpoints, so on DeepSeek the probe shows caching works but not that the breakpoints cause it; Opus (11/11, about 27× the cost) is the model that proves that.
Smoke output (2026-11-30)
Offline (--smoke), exit 0:
PASS turn 1 called lookup_section(7) PASS turn 1 answer quotes hollow-thistle PASS turn 2 called lookup_section(23) PASS turn 2 answer quotes kindled-cinder PASS turn 3 answer quotes hollow-thistle + kindled-cinder PASS every request carried cache_control breakpoints PASS every request used the pinned model PASS later requests read the cached prefix PASS without the compat no breakpoints are sent PASS with the compat breakpoints are sent PASS the pinned model caches with breakpoints PASS first request writes the prefix, reads nothing PASS the pinned model caches only with breakpoints PASS mock saw the bearer and X-Mycel-End-User on every request PASS wire tap saw every request the mock served 15/15 passed
Live, --model mycel:anthropic/claude-opus-5.5 --live --smoke, exit 0. Agent hit rate 99% on requests 2–5. Probe: 0 · 0 · 0 without breakpoints, 0 · 6612 · 6612 with them.
PASS turn 1 called lookup_section(7) PASS turn 1 answer quotes hollow-thistle PASS turn 2 called lookup_section(23) PASS turn 2 answer quotes kindled-cinder PASS turn 3 answer quotes hollow-thistle + kindled-cinder PASS every request carried cache_control breakpoints PASS every request used the pinned model PASS later requests read the cached prefix PASS without the compat no breakpoints are sent PASS with the compat breakpoints are sent PASS the pinned model caches with breakpoints 11/11 passed
Live with the default mycel:z-ai/glm-5.3-flash, exit 1. The agent read from cache on requests 3–5 (98–99%). The probe read 0 · 0 · 0 with breakpoints and 0 · 4096 · 0 without them:
FAIL the pinned model caches with breakpoints (reads with compat 0/0/0) 10/11 passed
An earlier GLM run passed the probe (0 · 0 · 19 with breakpoints). The live GLM result is best-effort, as noted above.
Live with the default mycel:deepseek/deepseek-v4.1-flash, two runs, both exit 0 and 11/11. Requests read 79% and 59% of prompt tokens from cache. Probe: 0 · 4224 · 0 without breakpoints, 0 · 0 · 4224 with them.