Module 05 · Lesson 5.3
Sequential, Parallel and Conditional Tool Execution
On this page
- What you will learn
- Three ways to order tool calls
- Read the dependencies from the plan and group steps into waves
- When is parallel execution safe? The rule and the limits
- Conditional steps: run only if an earlier result allows it
- What to do when one parallel call fails
- Compute waves and run them in Python
- Conclusion and your next step
- Common errors and fixes
- Validation checklist
- Hands-on exercise
- Knowledge check
- Frequently asked questions
- References
Imagine a laundry day. You cannot dry the clothes before you wash them. But the washing machine and the oven can both be working while you wait. And you only buy more soap if you find the box is empty. Three different kinds of order, all in one chore.
An agent's tool calls follow the same three patterns: sequential, parallel and conditional. In this lesson you learn to tell them apart, to read them from a plan, and to know when running together is safe. Most of the lesson needs no coding. One short Python example at the end groups a plan into waves and runs it.
What you will learn
- Tell sequential, parallel and conditional execution apart.
- Group the steps of a plan into waves using the
afterfield. - Apply the rule that only independent, read-only steps may run in parallel.
- Handle a failed parallel call without losing the other results.
Before you start
- The previous lesson, Plan-and-Execute Workflows.
- No coding experience is needed for most of this lesson.
- Optional: Python 3.10 or later, if you want to run the example yourself.
Three ways to order tool calls
A tool call is one request from the agent to a tool, such as "open document RS-01". When a plan has many tool calls, you must decide their order. There are three patterns.
- Sequential
- One step after another, because a step needs an earlier result. Like laundry: you wash first, and only then can you dry.
- Parallel
- Several independent steps run at the same time. Like three friends each carrying one bag from the car, instead of one person making three trips.
- Conditional
- A step runs only if a condition about earlier results is true. Like checking the fridge and buying milk only if it is empty.
We continue with the Quillfeather Cycles battery question from the last lesson. The agent reads documents RS-01 to RS-06 and saves a report at the end. The tools are search_docs, read_doc and save_report.
| Pattern | Battery research example | Why this order |
|---|---|---|
| Sequential | Use search_docs to find a document, then read_doc to open it. | You need the search result to know which document to open. |
| Parallel | Read RS-01 and RS-02 (the two spec sheets) together. | Neither one needs the other, and both only read. |
| Conditional | Read the returns log (RS-04) only if a spec sheet was found. | Without a spec sheet, the returns figures have nothing to compare with. |
Read the dependencies from the plan and group steps into waves
In Plan-and-Execute Workflows every step has an after list: the ids of steps that must finish first. That list is all you need to find the order. A step whose after steps are all finished is ready.
Steps that are ready at the same moment can form a wave. Run a wave, wait for all of it to finish, then look for the next wave.
{"id": 1, "tool": "read_doc", "args": {"doc_id": "RS-01"}, "after": []}
{"id": 2, "tool": "read_doc", "args": {"doc_id": "RS-02"}, "after": []}
{"id": 3, "tool": "read_doc", "args": {"doc_id": "RS-03"}, "after": []}
{"id": 4, "tool": "read_doc", "args": {"doc_id": "RS-04"}, "after": [1, 2]}
{"id": 5, "tool": "read_doc", "args": {"doc_id": "RS-06"}, "after": [2]}
{"id": 6, "tool": "save_report", "args": {"title": "Battery recommendation"}, "after": [3, 4, 5]}A plan with six steps. Only the fields that matter here are shown.
- Wave 1
Steps 1, 2 and 3. None waits for anything.
- Wave 2
Steps 4 and 5. Their
aftersteps are done. - Wave 3
Step 6, the save. It waits for a person.
Each wave starts only when the one before it has fully finished.
Because your code reads this from data, the agent does not have to guess. The after field tells it what is safe to do together.
When is parallel execution safe? The rule and the limits
"At the same time" needs a plain explanation. Most tool calls spend their time waiting, for example for a network reply. A computer can start the second call while the first is still waiting. So their waiting overlaps, and the total time is shorter. The calls do not need to be "truly" simultaneous to help.
Why so strict? Reading twice at once changes nothing, so the order does not matter. Writing is different. Imagine two people both updating the same shopping list at once. Each saves their own version, and one change is lost. In this module save_report is also gated by approval, so it never joins a parallel wave.
Two limits to set
- A maximum number of calls at once. Pick a small number, such as 3. Without a cap, a plan with 8 reads would fire all 8 together.
- The provider's rate limits. A rate limit is a cap that a service puts on how many requests you may send in a time window. If you go over, you get errors. Check the limits of every service your tools call.
Models can also ask for several tool calls in a single reply. Google's Function calling with the Gemini API (opens in a new tab) covers parallel function calls, and Anthropic's How tool use works (opens in a new tab) explains how a model asks for tools and gets results back. Even then, your code should still decide what actually runs together.
Conditional steps: run only if an earlier result allows it
A conditional step has a test attached. Your code runs the test on earlier results. If it is true, the step runs. If it is false, the step is skipped, which means it is marked as skipped and not run.
- Read the returns log only if a spec sheet was found.
- Read the workshop fitting notes only if the document before it was found.
- Ask a person for help only if two reads in a row failed.
Write the test in your own code, not in the model's words. A plain check such as "step 1 did not return an error" is easy to test and cannot be argued with. A skipped step still counts as finished, so steps that wait for it can go ahead.
What to do when one parallel call fails
Suppose three reads run together and one fails. The other two are still good. Do not throw them away.
- Collect each result separately. Keep a result for every step, whether it worked or not.
- Let the others count. Successful results stay in the run state.
- Turn the failure into an error observation. An observation is what the agent sees after a tool call. For a failure it is a short message such as
{"error": "no such document: RS-07"}, the same style you used in Tool Errors, Retries, Timeouts and Idempotency. - Then apply your failure rule. Stop, or re-plan within your limit, as in the previous lesson.
In code, the simplest way is to catch the problem inside each call and return it as a result. Then one bad call cannot break the whole wave.
Compute waves and run them in Python
The script below groups a plan into waves, runs each wave's read-only read_doc calls with up to three workers, skips conditional steps whose test is false, and prints the results in a fixed order. It uses a small in-memory copy of the documents, so it works offline. The plan asks for a document, RS-07, that does not exist. That shows a failure and a skip.
from concurrent.futures import ThreadPoolExecutor
DOCS = {
"RS-01": "Battery Alpha: 500 Wh capacity, 2.9 kg, supplier price 420, delivery in 6 weeks.",
"RS-02": "Battery Beta: 420 Wh capacity, 2.4 kg, supplier price 360, delivery in 3 weeks.",
"RS-04": "Last season, 6% of bikes with Battery Beta were returned for charging faults, compared with 2% for Battery Alpha.",
"RS-05": "Both batteries fit the same frame mount. Battery Alpha needs a firmware update before first use.",
}
def step(i, doc_id, after):
return {"id": i, "tool": "read_doc", "args": {"doc_id": doc_id}, "after": after}
steps = [step(1, "RS-01", []), step(2, "RS-02", []), step(3, "RS-07", []),
step(4, "RS-04", [1, 2]), step(5, "RS-05", [3]),
{"id": 6, "tool": "save_report", "args": {"title": "Battery recommendation"}, "after": [4, 5]}]
conditions = {4: lambda r: "error" not in r[1], 5: lambda r: "error" not in r[3]}
def make_waves(steps):
done, waves = set(), []
while len(done) < len(steps):
wave = [s for s in steps if s["id"] not in done and set(s["after"]) <= done]
waves.append(wave)
done |= {s["id"] for s in wave}
return waves
def run_read(s):
try:
return {"text": DOCS[s["args"]["doc_id"]]}
except KeyError:
return {"error": "no such document: " + s["args"]["doc_id"]}
results = {}
with ThreadPoolExecutor(max_workers=3) as pool:
for n, wave in enumerate(make_waves(steps), start=1):
print(f"wave {n}: steps {[s['id'] for s in wave]}")
skip = {s["id"] for s in wave if s["id"] in conditions and not conditions[s["id"]](results)}
reads = [s for s in wave if s["tool"] == "read_doc" and s["id"] not in skip]
results.update({s["id"]: r for s, r in zip(reads, pool.map(run_read, reads))})
for s in wave:
if s["id"] in skip:
results[s["id"]] = note = {"skipped": "condition is false"}
else:
note = "waits for a person to approve" if s["tool"] == "save_report" else results[s["id"]]
print(f" step {s['id']}: {note}")Runs offline with Python 3.10 or later. Run it with python run_waves.py.
What each part does
stepsis the plan. Step 3 asks for RS-07, which is not inDOCS. Step 5 waits for step 3.conditionsmaps a step id to a test. Step 4 runs only if step 1 has no error. Step 5 runs only if step 3 has no error.make_wavesrepeats one idea. A step is ready when every id in itsafterlist is already done. The ready steps form the next wave.run_readlooks up one document. If the ID is missing, it catches the error and returns an error observation instead of crashing.ThreadPoolExecutor(max_workers=3)is a small pool of helpers. It never runs more than 3 calls at once.pool.map(run_read, reads)runs the reads together, but gives the results back in the same order asreads. That is why the output is always the same.- Only
read_docsteps go to the pool. Thesave_reportstep does not run here. It waits for a person.
wave 1: steps [1, 2, 3]
step 1: {'text': 'Battery Alpha: 500 Wh capacity, 2.9 kg, supplier price 420, delivery in 6 weeks.'}
step 2: {'text': 'Battery Beta: 420 Wh capacity, 2.4 kg, supplier price 360, delivery in 3 weeks.'}
step 3: {'error': 'no such document: RS-07'}
wave 2: steps [4, 5]
step 4: {'text': 'Last season, 6% of bikes with Battery Beta were returned for charging faults, compared with 2% for Battery Alpha.'}
step 5: {'skipped': 'condition is false'}
wave 3: steps [6]
step 6: waits for a person to approveStep 3 failed, but steps 1 and 2 still delivered. Step 5 was skipped because its condition was false. Step 6 is alone in its own wave, and it waits for a person instead of running.
Conclusion and your next step
Use sequential order when a step needs an earlier result. Use parallel order for independent, read-only steps, with a cap on how many run at once. Use a condition when a step should run only if earlier results allow it. The after field gives you the waves, and each result is collected separately so one failure does not hide the rest.
Your next step: change max_workers to 1 and run run_waves.py again. The output is the same, because the order of results is fixed. Then continue to Human-in-the-Loop Approval and Escalation, where you design the approval gate that step 6 waits for.
For more on keeping an agent in bounds, see the Agent Launch Safety Checklist. You will use everything from this lesson in Practical Lab: Build a Multi-Step Research Agent.
Common errors and how to fix them
Two writes run in parallel and one overwrites the other.
Why it happens: Steps that change data were placed in the same wave.
Fix: Only send independent, read-only steps to the parallel pool. Run writes one at a time, after approval.
The provider returns rate-limit errors during a wave.
Why it happens: Too many calls were sent at once, with no cap.
Fix: Set a maximum number of calls at once, and check the provider's published limits.
One failed read stops the whole wave and the good results are lost.
Why it happens: The failure was raised as an exception and nobody caught it.
Fix: Catch problems inside each call. Return the failure as an error observation and keep every other result.
A conditional step runs when it should have been skipped.
Why it happens: The condition looks at the wrong step's result, or the check was left to the model.
Fix: Write conditions in code, point each one at the right step id, and test them with a missing document.
Validation checklist
Tick each item once you have confirmed it.
Hands-on exercise
Group a plan into waves
Here is a plan for the battery question. Work out the waves by hand. Then say how the waves change if you may run at most 2 calls at once, and which step is never run in parallel.
You will need
- Step 1: read_doc RS-01, after [].
- Step 2: read_doc RS-02, after [].
- Step 3: read_doc RS-03, after [].
- Step 4: read_doc RS-06, after [1, 2].
- Step 5: read_doc RS-04, after [2].
- Step 6: save_report "Battery recommendation", after [3, 4, 5].
Acceptance criteria
Your work is done when every one of these is true.
Hints (3)
- A step is ready when every id in its
afterlist is finished. - A limit on calls at once splits a wave into smaller groups. It does not change the order of waves.
- Which steps only read, and which one writes?
Show a model answer
Steps 1, 2 and 3 have empty after lists, so they are all ready in wave 1. Step 4 waits for 1 and 2, and step 5 waits for 2, so both are ready in wave 2. Step 6 waits for 3, 4 and 5, so it is wave 3. With at most 2 calls at once, wave 1 runs 1 and 2 together and then 3 on its own. Waves 2 and 3 stay the same. Step 6 saves a report, which is a write that needs approval, so it never goes to the parallel pool. If step 5 is made conditional on step 2, your code checks step 2's result first, and skips step 5 if that result is an error. A skipped step still counts as finished, so step 6 can still go ahead.
Knowledge check
Choose an answer, then check it. Nothing is recorded — this is for you.
Show the answer and explanation
Answer: A. Independent reads change nothing, so their order does not matter. Two writes to the same place are the tempting choice, but they can overwrite each other. A step that waits for a result cannot start before that result exists.
Show the answer and explanation
Answer: B. The two good results are still true and cost money to get. Starting again wastes them. Ignoring the failure hides a missing fact, which could lead to a wrong report.
Show the answer and explanation
Answer: C. The after list names the steps that must finish first. Running at the same time is the opposite, and it would break the order the plan describes. The ids in the list are not a repeat count.
Frequently asked questions
What is parallel tool calling?
It means running several tool calls so their waiting overlaps, instead of one after another. It is only safe when the calls do not depend on each other and do not change anything.
How many tool calls can an agent run at the same time?
As many as you allow, within the limits of the provider. Choose a small number, such as 3, set it in code, and check the rate limits of the services your tools call.
What is a conditional step in an agent plan?
It is a step that runs only if a test on earlier results is true. If the test is false, the step is skipped and the plan moves on.
Is parallel execution always faster?
No. It helps when calls spend time waiting, such as network requests. It adds little for quick calls, and it adds rate-limit and error-handling work. Use it for slow, independent reads.
- Written by
- PromptFable Editorial
- Last updated 18 Nov 2026
- Review
- Reviewed by Waseem Nadir, Technical Reviewer
- Last reviewed 27 Nov 2026
References
Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.
- Function calling with the Gemini API(opens in a new tab)
Google · Official documentation · Covers how a model asks for tools, including parallel function calls.
- How tool use works(opens in a new tab)
Anthropic · Official documentation · How a model asks for a tool and how your code returns the result.
- Building effective agents(opens in a new tab)
Anthropic · Official documentation · Describes parallel and routing patterns among simple agent workflows.
- concurrent.futures: launching parallel tasks(opens in a new tab)
Python Software Foundation · Official documentation · Background for `ThreadPoolExecutor` and `map` in the example.
- LLM06:2025 Excessive Agency(opens in a new tab)
OWASP · Official documentation · Why writes need limits and approval, not only reads.
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.