AI Agents
Build LLM-powered agents that plan, call tools, observe results, and iterate - enabling autonomous task completion beyond single-turn chat.
skill: AI Agents category: AI/ML tags: [AI Agents, Tool Use, ReAct, LangGraph, Autonomous AI] readingTime: 8
Agents add power but also unpredictability. Start with the simplest solution - a chain of LLM calls with hardcoded steps is more reliable than a fully autonomous agent. Only add agent autonomy when the task is genuinely dynamic and the branching logic is too complex to hardcode.
Workflow
- Define tools with clear schemasTools are functions the agent can call. Clear names, descriptions, and parameter schemas are the only information the LLM has to decide when and how to call them.
- Implement the ReAct loopReAct (Reasoning + Acting) is the standard agent pattern - Thought → Action → Observation repeating until the task is done.
- Add human-in-the-loop checkpointsFully autonomous agents make irreversible mistakes - sending emails, deleting files, making purchases. Pause before high-stakes actions.
- Use LangGraph for stateful multi-agent workflowsLangGraph models agent workflows as directed graphs with persistent state - better for complex multi-agent systems than a simple loop.
- Observe and limit agent behaviorWithout observability, debugging why an agent failed means parsing through dozens of tool calls. Without limits, runaway agents burn money and cause harm.
Output Format
Reliable agent with defined tools, ReAct loop, human-in-the-loop gates, and observability.
Agent:
├── Tools: clearly defined functions with docstrings
├── Loop: ReAct (Thought → Action → Observation)
├── Limits: max_steps, max_tokens, timeout
├── Gates: human approval for irreversible actions
└── Observability: every step loggedExamples
Input
Output
Edge Cases & Gotchas
- Prompt injection via tool output - Malicious content in tool results (web search, file read) can hijack the agent. Treat all tool output as untrusted. Never inject raw tool output into a system prompt.
- Infinite loops - Agents can get stuck in Thought → same Action → same Observation loops. Detect repeated tool calls with identical arguments and break the loop.
- Hallucinated tool calls - LLMs sometimes call tools with made-up argument names or wrong types. Validate all tool arguments against the schema before executing.
- State accumulation - Long agent runs accumulate messages and exceed context windows. Summarize conversation history after N turns to keep context manageable.
When to Break These Rules
- Use a deterministic pipeline instead of agents when the steps are known in advance - hardcoded step sequences are more reliable, debuggable, and cheaper.
- Don't give agents filesystem or shell access in production without sandboxing - use Docker or E2B sandbox for code execution.
Reference Files
https://langchain-ai.github.io/langgraph/https://www.anthropic.com/research/building-effective-agents