Replanning
When a step fails or produces unexpected results, the agent may need to replan. Replanning updates the remaining steps based on new information.
8 min•By Priygop Team•Updated 2026
When to Replan
- A required tool is unavailable: remove steps that use it and find an alternative
- A step returned unexpected data: the remaining steps may need different inputs
- An assumption was wrong: the plan assumed a resource existed, but it does not
- New information changes priorities: a search reveals the task can be completed in fewer steps
- A step failed despite retries: skip the failed step and use a fallback approach
Replan Implementation
Replan Implementation
# Replanning when a step fails
def replan_after_failure(
original_plan: list,
failed_step_id: str,
failure_reason: str,
available_tools: set
) -> dict:
"""
Generate a revised plan after a step failure.
"""
failed_step = next((s for s in original_plan if s["id"] == failed_step_id), None)
if not failed_step:
return {"status": "error", "error": f"Step {failed_step_id} not found"}
action = failed_step.get("action")
remaining_steps = [s for s in original_plan
if s["id"] != failed_step_id
and failed_step_id not in s.get("depends_on", [])]
# Strategy 1: Replace with a different tool that achieves the same result
tool_alternatives = {
"web_search": "document_search", # Fallback to internal docs
"send_email": "create_draft", # Create a draft instead of sending
}
if action in tool_alternatives:
alt_tool = tool_alternatives[action]
if alt_tool in available_tools:
revised_step = {**failed_step, "action": alt_tool,
"description": f"{failed_step['description']} (using fallback)"}
print(f"Replanning: replacing '{action}' with '{alt_tool}'")
return {
"status": "replanned",
"revised_plan": [revised_step] + remaining_steps,
"change": f"Replaced {action} with {alt_tool}"
}
# Strategy 2: Skip the step if it is non-critical
if failed_step.get("optional"):
print(f"Replanning: skipping optional step {failed_step_id}")
return {"status": "replanned", "revised_plan": remaining_steps,
"change": f"Skipped optional step {failed_step_id}"}
# Strategy 3: Escalate — cannot continue without this step
return {"status": "escalate",
"reason": f"Cannot complete goal: step {failed_step_id} failed — {failure_reason}"}
result = replan_after_failure(
[{"id": "S1", "action": "web_search", "depends_on": []},
{"id": "S2", "action": "summarise", "depends_on": ["S1"]}],
"S1", "Network timeout",
available_tools={"document_search", "summarise"}
)
print(result["status"], "—", result.get("change", result.get("reason")))Key Takeaways
- When a step fails or produces unexpected results, the agent may need to replan.
- A required tool is unavailable: remove steps that use it and find an alternative
- A step returned unexpected data: the remaining steps may need different inputs
- An assumption was wrong: the plan assumed a resource existed, but it does not