Execute a list of ordered plan steps by dispatching tools in sequence, accumulating observations, and returning a final answer.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement execute_plan(plan, tools) that runs a pre-defined sequence of tool calls.
The plan is a list of dicts, each with:
- tool (str): name of the tool to call.
- args (str): arguments string to pass to the tool.
- description (str): human-readable description of this step.
Requirements:
- Iterate through the plan steps in order.
- For each step, call tools[step["tool"]](step["args"]) and collect the result.
- Append Observation: <result>\n to an accumulated scratchpad string.
- If a tool name is not in tools, append Observation: Error -- unknown tool "<name>"\n instead.
- If a tool call raises an exception, catch it and append Observation: Error -- <message>\n, then continue to the next step.
- After all steps complete, call llm(scratchpad) to produce a final answer from the accumulated observations.
- Return the LLM's final response.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
import re
def execute_plan(plan: list[dict], tools: dict, llm) -> str:
scratchpad = "Executing plan:\n"Iterate plan steps, dispatch tools, accumulate observations
return llm(scratchpad)
Already have Pro access? Sign in