When to Chain LLM Calls: Only Where the Step Boundary Pays for Itself
Prompt chaining pays only where code can check a step, a step needs another model, or it must be logged alone. Agents find the path; workflows ship it.
Key takeaways
- Add an LLM hop only when code can check its output, it needs a different model or tool, or it must be logged on its own; otherwise use one call.
- Use agent loops to discover the steps, then compile the stable path into a typed workflow where code routes and the model only makes judgments.
- Merge a chain into one call only when per-question pass rates on the same eval set match, and keep the old step checks as validators on the final output.
On LexMed.ai and MetaCaddie, cost, latency and answer quality were the hurdles. Every extra model call moved all three at once, and rarely in the same direction. My rule came out of that. Chain LLM calls only where the boundary between two steps pays for itself. Everywhere else, use one call.
A boundary pays in three cases: code can check the step's output, the step needs a different model or tool, or the step has to be logged on its own. Most of the boundaries I keep exist so users can't abuse the system and don't have a bad time when a complex chain goes sideways. The ones I merge away are where the task turned out to be simple, or where a custom model does the heavy lifting the chain used to split up. Every other hop adds latency to every request and gives a bad output one more place to slip through unchecked.
The end-to-end camp says models eventually eat pipelines. Agent frameworks push toward more hops. I think both are right, at different stages of the same system. An agent finds the path, a typed workflow ships it, and a single call or a small custom model replaces it once the evals say it can.
Single LLM call vs prompt chaining vs agents
| Shape | p95 latency | Cost | Which step broke? | Audit |
|---|---|---|---|---|
| One call | One model round trip, and the only one you pay | Lowest. Context is sent once | You know the call failed, not which part of the task failed. On call, you rerun it and guess | One input, one output |
| Fixed chain with code checks | Sum of the hops on the critical path | Tokens per hop, and shared context is sent again on every hop | The check that failed names the step | Each step logged on its own |
| Agent loop | No ceiling. Depends on how many turns it takes | No ceiling, for the same reason | Read the whole trajectory | Only if you log every turn |
The agent loop is the only shape where you can't write down the worst case before it runs. That's fine for exploration. It's a problem when a person is waiting.
Prompt chaining vs a single prompt: when a hop pays for itself
Anthropic's Building effective agents defines prompt chaining as a sequence of calls where each one processes the previous output, with optional code checks in between. It says chaining trades latency for accuracy. It also says that for many applications, a single call with retrieval and in-context examples is usually enough. It never says where the line falls. The same post notes that a guardrail call run in parallel with the core response tends to beat one call doing both. Here's where I draw the line.
Code can check the output. The output validates against a schema. Every code exists in the code set. Every resource ID belongs to this patient. Say step one extracts medication codes and step two writes a recommendation. The boundary between them is where you catch a made-up code before anything builds on it. If you can't write a check, the boundary buys nothing, because the next model treats the error as fact.
The step needs a different model or tool. A router or classifier runs fine on a small, fast model. Generation may need the big one. A FHIR read or a retrieval call can sit between two judgments. Splitting at those points means you pay flagship prices only where you need flagship quality.
The step must be logged on its own. In clinical systems, every write needs an audit trail. If a reviewer has to see what the model decided before anything was written, that decision needs its own record. The audit record has to point at a specific step, not at "the chain."
Abuse checks are the clearest case. Deciding whether a request is something the system should answer at all is a separate judgment with a separate job. Its output is a yes or no that code can act on: block, or continue. That boundary also handles the "bad time" problem. When a check fails, code returns a clear refusal or a fallback. A broken intermediate result never reaches the user. Run it in parallel with the main call and it doesn't add a hop to the critical path.
My test for any proposed hop is one question: what does code do with this step's output before the next step sees it? If the answer is "pass it along," delete the hop.
Agents vs workflows: explore with one, ship the other
Start with an agent loop on real inputs. Log every trajectory: each tool call with its inputs and outputs, the model's stated reason for the call, the turn count and the time per turn. Then read the logs and group runs by their tool sequence. The sequence most runs share is your workflow. Branches that rarely appear become explicit cases in code, or a handoff to a human. Don't compile until that dominant sequence stops changing as you add new runs. If every batch of new inputs reshapes the path, you don't have a workflow yet. I go deeper on agent failure modes in What It Actually Takes to Build a Real LLM Agent.
Then compile that path into a typed workflow. Code does the routing. The model is called only at judgment points.
def triage(event):
note = fetch_note(event.note_id) # code
findings = llm_extract(note, schema=Findings) # judgment
check_codes(findings.codes) # code: codes exist in the code set
check_ids(findings.resource_ids, event.patient) # code: IDs belong to this patient
if findings.severity == "none":
return None # code routes, not the model
return llm_draft(findings, model="small") # judgment, cheaper model
That's two model calls and four code steps. Every boundary has a check or a model switch behind it.
Write this yourself. Don't reach for an agent framework. When it breaks, you need the exact prompt and the exact response for the hop that failed, and framework abstraction layers hide exactly that. A plain function you wrote is something you can debug from a trace while on call.
Agent loops aren't wrong in production. My support bot is Claude Code on a five-minute loop. That works because nobody is waiting on it and a human merges every PR. Put the same loop in a clinician's request path and the missing latency ceiling becomes a real problem. Running several agents side by side is a separate question, covered in Multi-Agent Orchestration in Practice.
Once the workflow exists and you have a labeled eval set, DSPy-style prompt optimization can tune each judgment point against that set. Optimize before the eval set exists and you're tuning against vibes. It's the refine-and-retry loop moved out of your head and into infrastructure.
LLM chaining latency: budget it per hop
CDS Hooks is the strictest budget I know. The spec says services SHOULD respond quickly, on the order of 500 ms. That's a SHOULD, not a hard limit. The timeout an EHR actually enforces is configured per organization. Secondary sources quote anywhere from 300 ms to 10 s for Epic, with nothing primary behind any of those numbers. Check what your organization actually has configured, and design for 500 ms.
No chain fits in that window, so the chain doesn't run there. It runs earlier, when the triggering event arrives, and writes its result to a cache keyed by patient and encounter. The hook does a cache read. If the cache is empty, the hook returns nothing. More on keeping the model out of that path in Make the Hook Say Nothing.
The precompute job still has a budget, because the cache has to be warm before the clinician opens the chart. I'm not going to fill this section with p95s I made up. Your numbers depend on your models, your FHIR server and your prompts, so measure them. Two rules make the measurements useful.
First, report p95 per hop, not just the total. An end-to-end number tells you that you're slow. It doesn't tell you where.
Second, fan out independent steps. With a parallel fan-out, the critical path is the slowest branch, not the sum of the branches.
async def precompute(patient_id, encounter_id):
meds, labs, problems = await asyncio.gather(
timed("meds", summarize_meds(patient_id)),
timed("labs", summarize_labs(patient_id)),
timed("problems", summarize_problems(patient_id)),
)
cards = await timed("compose", compose(meds, labs, problems))
cache.set((patient_id, encounter_id), validate(cards))
Each timed call emits its own span. When the total p95 moves, one span moves with it, and that span names the hop. The cache read in the hook gets its own span too, because that's the number the clinician actually feels.
Spending more compute inside a single call is a different lever, covered in Trading Speed for Quality. This budget is about hops.
When to merge LLM calls back into one
Karpathy's Software 2.0 argues that modules "meld into an optimal whole" because you can backpropagate through all of them. He limits the claim to domains where repeated evaluation is possible and cheap. Two more points from that post matter here. He lists constant running time as a benefit: every forward pass costs the same FLOPS, so you never land in an unintended infinite loop. That's the opposite of an agent loop, and it's why a merged call is easier to budget than the chain it replaces. He also concedes the cost. Teams end up choosing between a 90% accurate model they understand and a 99% accurate one they don't. I've written about what that shift changed for me.
Chained API calls joined by text and code checks aren't differentiable, so nothing melds them for you. Evals tell you when a merge is safe. The signal to collapse is that your eval set shows one call, or a small fine-tuned model, matches the chain's quality. That's what "simple enough" or "a custom model does the heavy lifting" looks like in practice.
"Matches" needs a definition before you run anything. Mine has three parts:
- Run the chain and the merged call on the same eval set.
- Compare pass rates per question, not one average. An average can hide one question dropping hard while others rise.
- The deterministic checks have to pass on the merged output at least as often as they did on the chain.
def can_merge(chain, merged, evalset, tol):
a = pass_rates(chain, evalset) # per question
b = pass_rates(merged, evalset)
return all(b[q] >= a[q] - tol for q in a) and \
check_rate(merged, evalset) >= check_rate(chain, evalset)
Pick tol before you look at the results, not after.
When you merge, keep the old step checks. They stop running between hops and start running once, against the final output. The schema check, the code-set check and the patient-ID check all survive. You lose per-step blame, but you keep the checks. That's how you take the 99% model without flying blind on the cases it gets wrong. Keep the old chain too, as the baseline the next regression gets compared against.
Generating a whole document in one call is fine under one condition: every claim in the output can still be traced to its source. If you can split the output into claims and cite each one to a record, the merged call is auditable. If you can't, you traded an audit trail for a few hundred milliseconds. That's a bad trade in any system where someone has to sign the output. The claim-level check is in detecting hallucinations in AI clinical notes.
Questions people ask
When should I chain LLM calls instead of using one prompt?
Chain only when the boundary pays for itself: code can check the step's output (schema, codes, IDs), the step needs a different model or tool, or the step must be logged on its own. If the next step just receives the output unchecked, delete the hop.
Should I use an AI agent or a workflow in production?
Use an agent loop to explore real inputs and log its trajectories. Then compile the common tool sequence into a typed workflow where code does the routing. Agent loops have no latency ceiling, so they fit jobs nobody is waiting on, not a clinician's request path.
How do I fit an LLM chain into the CDS Hooks 500 ms response time?
Don't run the chain in the hook. Precompute it when the triggering event arrives, cache the result by patient and encounter, and have the hook do a cache read that returns nothing if the cache is empty. The 500 ms figure is a SHOULD in the spec, and the enforced timeout is configured per organization.
When should I merge chained LLM calls back into a single call?
Merge when the chain and the merged call, run on the same eval set, show matching per-question pass rates within a tolerance you pick in advance, and the deterministic checks pass at least as often. Keep the old step checks running against the final output, and keep the old chain as a baseline.