Building AI Agents With Tool Calling: Planning, Memory and Error Handling

Building AI agents with tool calling: the five-step loop, planning, layered memory and error handling that keeps agents from failing silently.

By Galactic News Editorial · Published 2026-11-23T01:44:54+00:00 · Updated 2026-11-23T01:44:54+00:00

Building AI agents with tool calling, planning, memory and error handling

Building AI agents with tool calling turns a language model into a program that can act. The model reads a request, picks a tool, and your code runs it. The result goes back to the model, which decides the next step. This guide explains the loop, how to add planning and memory, and how to handle errors so the agent recovers instead of failing.

Building AI agents tool calling: the five-step loop

Tool calling, also called function calling, lets a model interface with external systems. You declare each tool with a name, a description and a JSON schema for its arguments. The model never runs the tool. It only returns a structured request to call it.

OpenAI's documentation describes a five-step flow. First, you send a request with the tools the model may call. Second, the model returns a tool call. Third, your code executes the function with the arguments the model supplied. Fourth, you send the tool output back to the model. Fifth, the model returns a final answer, or more tool calls.

Anthropic's tool use works the same way with different names. You pass a tools list in a messages request. When Claude wants a tool, the response has a stop reason of tool_use and one or more tool_use blocks. Your code runs the operation and sends back a tool_result block. The loop repeats until the model answers in plain text.

The description is the most important part of a tool definition. The model decides when to call a tool from its description. Vague descriptions produce wrong calls. Write each description like a contract. Say what the tool does, when to use it, and when not to use it.

Planning before acting

A simple agent reacts one step at a time. It calls a tool, reads the result, and picks the next tool. That works for short tasks. Longer tasks need a plan.

Planning means breaking a goal into steps before acting. The agent writes a list of steps, works through them, and marks each one done. LangChain's deep agents include a built-in write_todos tool for exactly this. The agent tracks each step from pending to in progress to completed.

A plan also gives the user something to inspect. If the plan is wrong, the user can correct it before the agent spends money or changes data. For tasks with side effects, such as sending email or updating a database, show the plan first and require approval before execution.

Keep plans short. Five to seven steps cover most tasks. If a plan grows past ten steps, split the task into smaller jobs. Each job gets its own agent run and its own plan.

Memory: short-term, long-term and retrieval

An agent without memory starts every conversation from scratch. Memory comes in layers, and each layer solves a different problem.

Working memory is what the model sees on the current call. It is the messages array. Keep it small. Only the recent turns and the latest tool results belong here.

Short-term memory covers one conversation. In LangGraph, a checkpointer saves the full state under a thread ID after every call. The next call with the same ID restores it. This keeps context across turns without resending the whole history by hand.

Long-term memory covers facts across conversations. A store holds user preferences and project facts in a namespaced key-value store. LangChain's deep agents add a memory system that loads persistent notes into the system prompt, similar to a project file that survives across sessions.

Semantic memory finds the relevant fact in a large history. A vector store wrapped as a tool lets the agent search past conversations and documents. The agent calls it only when it needs background, instead of loading everything into the prompt.

Choose the layer by the question it answers. Working memory answers what is happening now. Short-term memory answers what happened in this conversation. Long-term memory answers what is true about this user. Semantic memory answers what did we learn before that applies here.

Error handling that keeps agents alive

Agents fail in predictable ways. A tool returns an error. The model calls a tool with bad arguments. A tool result is too large. The agent loops forever. Each failure needs a different response.

Start with a hard iteration cap. Ten loops is a sensible default for most tasks. Without a cap, a confused agent can call tools until the budget runs out. When the cap hits, stop and report what was done and what remains.

Validate arguments before running a tool. If a required field is missing, return an error to the model and let it retry with corrected arguments. OpenAI's strict mode constrains the model's output so it always matches the schema. Anthropic offers the same with strict tool schemas. Use strict mode in production.

Return errors as tool results, not as crashes. In Anthropic's API, mark a failed result with is_error set to true. The model reads the error and can correct its arguments, try a different tool, or tell the user what went wrong. In OpenAI's Agents API, send a tool result with success set to false and an error message the agent can use.

Watch result sizes. OpenAI rejects tool results larger than 4 MiB. Trim large results before returning them. Summarise a long list, or return the top results with a count of the rest.

Log every tool call. Record the tool name, the arguments, the result and the time. When an agent does something unexpected, the log shows exactly where it went wrong. Without logs, debugging an agent is guesswork.

A starter checklist for your first agent

  1. Define three tools or fewer. Start small. A search tool, a read tool and a write tool cover most first agents.
  2. Write strict schemas. Use strict mode so arguments always validate.
  3. Cap the loop at ten iterations. Stop and report if the cap is reached.
  4. Require approval for side effects. Reads are free. Writes, sends and deletes need a human or a policy check.
  5. Add short-term memory first. A checkpointer gives you conversation continuity with the least work.
  6. Log everything. Tool calls, arguments, results and errors go to a structured log.
  7. Test with a fixed eval set. Ten realistic tasks with expected outcomes. Run them after every change.

Bottom line

An agent is a model, a set of tools and a loop. Tool calling supplies the tools. Planning keeps multi-step work on track. Memory supplies context across turns and sessions. Error handling keeps the agent from looping forever or failing silently. Start with a small tool set, strict schemas and a hard iteration cap. Add planning and memory only when a real task needs them. The simplest agent that completes the job is the right one.