Overview
Designs and implements reliable multi-step AI agent chains. Covers chain types (sequential, parallel, branching, looping), tool definition (JSON schema + execution), planning patterns (ReAct, Plan-and-Execute, Reflexion), error recovery (retry, fallback, human-in-the-loop), state management, observability (tracing), and complete implementation templates using LangChain, LlamaIndex, or custom code.
When to Use This Skill
- Building agents that need to use tools, plan, or perform multi-step reasoning.
- The user describes a complex task that requires "research then act", "use these tools in sequence", or "handle errors gracefully".
- Existing single-prompt or simple RAG approaches are insufficient.
Prerequisites
- Clear task that benefits from tool use or multi-step reasoning.
- Tools/APIs the agent will call (with authentication).
- LLM that supports tool calling / function calling well (Claude 3, GPT-4, Gemini 1.5, etc.).
- (Recommended) LangChain, LlamaIndex, or a custom agent framework.
Steps
Decompose the task:
- What are the atomic steps?
- Which steps require tools vs pure reasoning?
- What are the failure modes and how should the agent recover?
Define tools (critical):
- Name, description (the model reads this), parameters (JSON Schema).
- Implementation (the actual function the agent calls).
- Return value format (the model sees this).
- Error handling inside the tool (never let the tool crash the agent).
Choose agent architecture:
- ReAct (Thought → Action → Observation loop) — very popular and effective.
- Plan-and-Execute (high-level plan first, then execute steps).
- Reflexion / self-critique (agent critiques its own output and retries).
- Hierarchical (manager agent + specialist agents).
State & memory:
- Short-term (conversation history, scratchpad).
- Long-term (vector store of past experiences or facts).
- How to summarize or prune history when context grows.
Error recovery & control flow:
- Retry with different prompt or tool.
- Fallback to simpler method or human.
- Max iterations / timeout to prevent infinite loops.
- Structured output for control (e.g., "next_action": "tool" | "final_answer" | "ask_human").
Observability:
- Log every thought, action, observation, and final answer.
- Use LangSmith, Phoenix, Helicone, or custom tracing.
- Capture token usage and latency per step.
Output:
- Tool definitions (JSON Schema + Python functions).
- Agent prompt(s) (system + ReAct format).
- Full agent loop code (or LangChain/LlamaIndex agent).
- Error handling and recovery logic.
- Example run trace (good and bad paths).
- Evaluation approach for the agent.
Examples
A complete ReAct-style agent for "research a company and draft a personalized outreach email" with tools for web search, company info lookup, email draft, and human approval step, including full prompts, tool schemas, the agent loop, and tracing is included.
Edge Cases & Error Handling
- Tool returns garbage: Agent should detect low-quality observation and either retry the tool with better params or fall back.
- Infinite loops: Hard max_steps + timeout.
- Cost explosion: Track cumulative tokens/cost; stop early if budget exceeded.
- Hallucinated tool calls: The framework should only allow calling tools you defined.
Verification
- The agent successfully completes the happy-path task using tools.
- On a failing tool or bad intermediate result, it recovers (retries, falls back, or asks for help).
- Traces are complete and readable (you can debug why it did what it did).
- It does not loop forever or spend excessive tokens.
- On a held-out set of tasks, success rate is measured and acceptable.
- Success: The agent reliably accomplishes the multi-step goal with appropriate tool use and graceful handling of problems.
References
1---2name: agent-chain-designer3description: Designs multi-step AI agent chains with tool use, planning, and error recovery. Use when building agentic systems that require planning, tool calls, or multi-turn reasoning.4license: Apache-2.05---67## Overview89Designs and implements reliable multi-step AI agent chains. Covers chain types (sequential, parallel, branching, looping), tool definition (JSON schema + execution), planning patterns (ReAct, Plan-and-Execute, Reflexion), error recovery (retry, fallback, human-in-the-loop), state management, observability (tracing), and complete implementation templates using LangChain, LlamaIndex, or custom code.1011## When to Use This Skill1213- Building agents that need to use tools, plan, or perform multi-step reasoning.14- The user describes a complex task that requires "research then act", "use these tools in sequence", or "handle errors gracefully".15- Existing single-prompt or simple RAG approaches are insufficient.1617## Prerequisites1819- Clear task that benefits from tool use or multi-step reasoning.20- Tools/APIs the agent will call (with authentication).21- LLM that supports tool calling / function calling well (Claude 3, GPT-4, Gemini 1.5, etc.).22- (Recommended) LangChain, LlamaIndex, or a custom agent framework.2324## Steps25261. **Decompose the task**:27 - What are the atomic steps?28 - Which steps require tools vs pure reasoning?29 - What are the failure modes and how should the agent recover?30312. **Define tools** (critical):32 - Name, description (the model reads this), parameters (JSON Schema).33 - Implementation (the actual function the agent calls).34 - Return value format (the model sees this).35 - Error handling inside the tool (never let the tool crash the agent).36373. **Choose agent architecture**:38 - ReAct (Thought → Action → Observation loop) — very popular and effective.39 - Plan-and-Execute (high-level plan first, then execute steps).40 - Reflexion / self-critique (agent critiques its own output and retries).41 - Hierarchical (manager agent + specialist agents).42434. **State & memory**:44 - Short-term (conversation history, scratchpad).45 - Long-term (vector store of past experiences or facts).46 - How to summarize or prune history when context grows.47485. **Error recovery & control flow**:49 - Retry with different prompt or tool.50 - Fallback to simpler method or human.51 - Max iterations / timeout to prevent infinite loops.52 - Structured output for control (e.g., "next_action": "tool" | "final_answer" | "ask_human").53546. **Observability**:55 - Log every thought, action, observation, and final answer.56 - Use LangSmith, Phoenix, Helicone, or custom tracing.57 - Capture token usage and latency per step.58597. **Output**:60 - Tool definitions (JSON Schema + Python functions).61 - Agent prompt(s) (system + ReAct format).62 - Full agent loop code (or LangChain/LlamaIndex agent).63 - Error handling and recovery logic.64 - Example run trace (good and bad paths).65 - Evaluation approach for the agent.6667## Examples6869A complete ReAct-style agent for "research a company and draft a personalized outreach email" with tools for web search, company info lookup, email draft, and human approval step, including full prompts, tool schemas, the agent loop, and tracing is included.7071## Edge Cases & Error Handling7273- **Tool returns garbage**: Agent should detect low-quality observation and either retry the tool with better params or fall back.74- **Infinite loops**: Hard max_steps + timeout.75- **Cost explosion**: Track cumulative tokens/cost; stop early if budget exceeded.76- **Hallucinated tool calls**: The framework should only allow calling tools you defined.7778## Verification79801. The agent successfully completes the happy-path task using tools.812. On a failing tool or bad intermediate result, it recovers (retries, falls back, or asks for help).823. Traces are complete and readable (you can debug why it did what it did).834. It does not loop forever or spend excessive tokens.845. On a held-out set of tasks, success rate is measured and acceptable.856. Success: The agent reliably accomplishes the multi-step goal with appropriate tool use and graceful handling of problems.8687## References8889- [ReAct Paper](https://arxiv.org/abs/2210.03629)90- [LangChain Agents](https://python.langchain.com/docs/modules/agents/)91- [LlamaIndex Workflows / Agents](https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/)92- [Reflexion Paper](https://arxiv.org/abs/2303.11366)93- [Toolformer Paper](https://arxiv.org/abs/2302.04761)94- [LangSmith](https://docs.smith.langchain.com/)