Flows category icon
Agent Architecture
Advanced

Make Parallel Sub-Agents Share the Prompt Cache

Byte-identical request prefixes are why forked workers cost a fraction of spawned ones - engineer your dispatch so every parallel child hits the same cache entry.

4 steps12 verify checks60-120 minutesWorks with: emergent · chatgpt · claude · cursor
Start Guided Walkthrough

The Route

0/4 verified

Context Pack

Paste this first. It briefs the AI on requirements, constraints, and the Definition of Done before your first build prompt.

Context Pack
PROJECT CONTEXT:
My parallel sub-agents each pay full input-token price on every request, making fan-out prohibitively expensive. Claude Code's fork mode exists to fix exactly this: all engineering choices - placeholder results, exact tool arrays, inherited configs - serve making sibling request prefixes byte-identical so they share one cache entry.

GOAL:
Engineer fork dispatch for maximum prompt-cache reuse: stable prefixes, identical placeholders, exact configuration inheritance, and measured cache-hit rates.

REQUIREMENTS:
- Sibling fork requests identical in every byte up to the per-child directive
- Placeholder tool-results with fixed, shared text for pending parent tool calls
- Tool definitions passed through exactly - no re-filtering, re-ordering, or re-serialization
- Model-configuration inheritance (thinking/verbosity settings) from the parent
- Cache-hit metrics per dispatch batch proving the savings are real

CONSTRAINTS:
- Any 'harmless' prefix difference (timestamp, ordering, whitespace) silently zeroes the savings
- The differing content (the directive) must come last in the request

DEFINITION OF DONE:
- A byte-level diff of two sibling requests shows differences only in the final directive block
- Provider metrics show cached input tokens dominating for second-and-later siblings
- Fan-out cost scales near-flat with sibling count instead of linearly
- A prefix-stability regression test guards the dispatch path

COMMON FAILURES TO AVOID:
- Per-child placeholder text ('processing task 1 of 3') breaking prefix identity
- Re-running tool filtering for each child, changing array order and killing the cache
- Injecting the child directive before the shared history instead of after
- Assuming savings without measuring - one serialization change and you pay full price silently

Paste this into your AI builder first. It teaches the AI what you want before you give it the build prompt.

Related routes

More Agent Architecture flows that share ground with this one.

Flows category icon
Agent Architecture

Choose the Right Dispatch: Spawn, Fork, or Direct

Three ways to hand work to a sub-agent - fresh-context spawn, cache-sharing fork, or just doing it yourself - and the decision matrix for picking per task.

01Implement spawn: fresh context specialists
02Implement fork: inherited context, shared cache
03Add guards and async lifecycle

+1 more steps to Done

Best forproduction-grade builds with strict verification

4 steps90-150 minutesAdvanced
Flows category icon
Agent Architecture

Sub-Agent Delegation with Context Isolation

Delegate bounded work to sub-agents that run in fresh context and return only distilled results - the pattern behind scalable long tasks.

01Specify the delegation contract
02Implement the isolated sub-loop
03Wire dispatch into the parent and distill

+1 more steps to Done

Best forproduction-grade builds with strict verification

4 steps90-150 minutesAdvanced
Flows category icon
Agent Architecture

Add a Plan/Todo System to Your Agent

Give your agent the planning discipline of the leading harnesses: an explicit, model-visible todo list that survives long tasks.

01Design the todo data model and tool
02Render the plan into context every turn
03Enforce plan-first and completion gates

+1 more steps to Done

Best forbuilders who have shipped a basic app before

4 steps60-90 minutesIntermediate

Made with Emergent