Dec 2
AI Agent Loop Design: Tools, Hooks, and Context

AI agents like Claude Code are now part of everyday work across a wide range of roles. At Squadbase, one of our core features is an AI agent built for data work.
Still, "AI agent" is a broad term, and what people mean by it keeps shifting. Drawing on our experience building an agent for data work, this post breaks down how AI agents are designed and implemented: how an AI agent loop differs from a graph-based workflow, how tools and the harness shape what an agent can do, and what changes when you build an agent for one domain.
AI agent loops vs. graph-based workflows
While there's no strict definition, an AI agent is generally understood as a system that uses an LLM to carry out tasks autonomously on a user's behalf. In other words, it fulfills a user's request by making multiple LLM calls, and we can classify agent implementations by how those calls are organized. The layer that controls an agent's behavior is often called the harness, and we'll also look at what the harness actually refers to in each pattern.
Graph-based agents (e.g., Dify, n8n)

In a graph-based agent, such as one built with Dify or n8n, you define the task's execution graph upfront. The agent then completes the task autonomously by passing each step's output to the next.
A node can also run custom logic alongside its LLM call, such as a request to an external API, which lets you combine scripted logic and LLM calls cleanly.
In a graph-based agent, the graph design itself and the rules for passing outputs between nodes make up the harness.
Tool-loop agents (e.g., Claude Code, Codex)

A tool-loop agent calls the LLM repeatedly with the accumulated history and keeps going until a stop condition is met. Each iteration is called a step. In a step, the model can reply to the user or request one of the tools it was given; when it requests a tool, the harness runs it and adds the result to the history.
If we write the output of step n as O_n and the harness as h(), the output of step n+1 looks like this:
O_(n+1) = h(O_1, ..., O_n)This simple formula is the essence of a tool-loop agent. (In practice there are many constraints, so this is a heavy simplification.)
In a tool-loop agent, the harness decides when to stop, which tools to make available, and what to run before and after each step.
Why the tool loop dominates
There are other ways to design an agent, but most are still experimental. The agents used in production today mostly fall into the graph-based or tool-loop pattern, and of the two, the tool loop is by far the more widely used.
For tasks you can define upfront, a graph is more efficient in both cost and time. For tasks that are hard to specify in advance, though, it quickly breaks down. It simply lacks flexibility.
Because a tool-loop agent relies on the LLM's reasoning and judgment at every step, a single harness can handle a wide variety of tasks, and you can extend what the agent can do just by giving it tools. On the other hand, it sends the entire history of past steps to the LLM, so the context tends to bloat and cost efficiency suffers.
| Pattern | Characteristics | Handling a new task |
|---|---|---|
| Graph-based | Less flexible, but efficient | Rebuild the graph itself |
| Tool loop | Highly flexible and extensible, but costs more | Keep one harness and give it tools |
That flexibility, extensibility, and portability are what make the tool loop the dominant pattern.
Extending an agent with tool calls
In simplified pseudocode, giving a tool-loop agent the ability to read local files looks like this:
import { readFile } from "node:fs/promises";
const tools = {
readFile: {
description: "Read a file",
execute: (path: string) => readFile(path, "utf-8"),
},
};
while (true) {
const response = await LLM(history, tools);
history.push(response);
if (!response.toolCall) break;
const { name, args } = response.toolCall;
const result = await tools[name].execute(...args);
history.push(result);
}Because you can put any logic you want inside execute, you can add virtually any capability this way.
The extensions used in Claude Code and similar agents (agent skills, MCP, subagents, and memory) are all implemented as tools, too.
const tools = {
// Agent Skills
loadSkill: {
description: "Load an agent skill",
execute: (name: string) =>
readFile(`skills/${name}/SKILL.md`, "utf-8"),
},
// MCP (Notion)
searchNotion: {
description: "Search Notion",
execute: (query: string) =>
MCP("https://mcp.notion.com/mcp").callTool({
name: "notion-search",
arguments: { query },
}),
},
// Subagent
callSubagent: {
description: "Delegate a task to a subagent",
execute: (prompt: string) => {
const subagentTools = {...}
while (true) {
....
}
},
},
};Pass the agent a set of tool descriptions and their implementations, and it calls them as needed.
Inside the agent harness
Beyond tools, a few other pieces shape how the harness behaves:
- System prompt
- Hooks: logic that runs before or after a turn or a step. Compacting the history and formatting tool results are typical examples.
- Stop conditions: a common pattern is to provide an "ask the user a question" tool and end the agent's turn when it's called.
- Execution environment: where the agent runs. Claude Code runs on the user's machine; Squadbase runs on cloud servers.
Designing a domain-specific AI agent
As we've seen, a tool-loop agent's capabilities are extended through tools. By exposing workflow-specific operations as an API and providing them as tools, you can tailor an agent to a specific business domain. MCP is simply a shared protocol for providing those tools, which is why an agent gains customer-management capabilities when you connect a CRM's MCP server, and content-management capabilities when you connect a CMS's.
If that's the case, why not just keep plugging tools into the best general-purpose harness? Isn't Claude Code all you need? It's a fair question.
So let's look at why a domain-specific agent like Squadbase is needed, and what goes into building one.
Execution environment
Squadbase builds an agent for teams that work with data. That requires:
- A cloud execution environment shared by team members
- Network controls in that environment that keep the data it handles from leaking out
The agent also needs both context and tools that tell it what environment it's running in and what it can control there.
Domain-specific tools
Narrowing the scope of tasks lets you offer more specific tools. For example, instead of having the agent run SQL through bash commands, you can give it a dedicated tool that runs queries against Snowflake. A dedicated tool can check query syntax before execution or return large result sets in chunks, making tool use more efficient and more reliable.
One example in Squadbase is shared data source connections. Because connections need to be shared across a team, connection handling runs on the server, and the agent gets dedicated tools to work with it.
Adding tools extends what an agent can do, but every tool definition is sent in full with every LLM call, so the more tools you add, the more the context bloats. A bloated context increases cost and latency, and it's also known to degrade model quality, a phenomenon called context rot. In other words, more tools aren't automatically better.
Narrowing the task domain lets you keep the loaded toolset lean. Combined with lazy loading and offloading work to subagents, that avoids the bloat and keeps performance high.
Multi-model routing
A general-purpose agent can't know what it will be asked to do, so always using the most capable model tends to become the default. Yet translating a user's request into SQL, for example, doesn't need the latest Claude Opus model.
A domain-specific agent, on the other hand, can anticipate much of its workload. In Squadbase's case, that means data exploration, analysis and insight discovery, and building dashboards. Routing those predictable tasks to a lighter model that fits the job cuts costs for users and speeds up the agent, while high-end models are reserved for everything else.
Context management and compaction
In a chat with an agent, once the message history grows too long, performance drops or requests fail because they exceed the context window. Dealing with this requires compressing the history, a process known as compaction.
The concept is simple, but there are many ways to implement it. As of 2026, common approaches include:
- Passing the history to an LLM to produce a text summary
- Keeping only the most valuable steps from the history
- Setting a character limit on each message and tool output, and truncating whatever exceeds it
Even for compaction alone, the right choice depends on what the agent is for.
In Squadbase's case, for example, summarizing the message history as free text would lose the exact details of SQL results that the agent needs to answer accurately. So we use compaction that combines several rules.
Specializing an agent for a clear purpose opens up deeper optimizations in the harness, too.



