---
title: "What is the difference between a chat completion and an agent?"
description: "A chat completion is one request and one reply. An agent is a loop where the model calls tools, sees results and decides what to do next until the job is done."
url: https://www.danny.engineering/answer/ai/what-is-the-difference-between-a-chat-completion-and-an-agent
author: Danny
published: 2026-12-03T00:00:00.000Z
updated: 2026-12-03T15:34:00.000Z
tags: ["AI"]
---

# What is the difference between a chat completion and an agent?

## The short answer


A chat completion is a single call: you send a prompt, the model sends back text, and that is the end of it. An agent is a loop: the model is given tools, it asks you to run one, you send the result back, and it decides whether to call another tool or finish. The model drives the sequence of steps. A completion is a function; an agent is a program that keeps calling that function until the work is done.


```typescript
// Chat completion: one call, one answer
const response = await client.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'What time zone is Lisbon in?' }],
});
```


## How it works


**A single call has no hands.** A completion can only produce text from what is in the prompt. If the answer needs today's weather, a row from your database or the output of a test run, the model has to guess or tell you it cannot know. Everything it needs must be in the request already.


**Tools give the loop a reason to exist.** A tool is a function you describe to the model: a name, a description and a JSON schema for its input. Instead of answering straight away, the model can return a `tool_use` block saying which tool to call and with what arguments. You run the function, send the result back as a `tool_result`, and call the API again. Now the model has new information it did not have before, and it can use it, or ask for more. Without tools, looping would just produce more text; with tools, each pass through the loop can change what the model knows.


**The loop in code.** The signal is `stop_reason`. When it is `tool_use`, the model wants something run. When it is `end_turn`, it has finished. Return every tool result from one turn in a single user message.


```typescript
const messages: Anthropic.MessageParam[] = [{ role: 'user', content: task }];

while (true) {
  const response = await client.messages.create({
    model: 'claude-opus-5-5',
    max_tokens: 4096,
    system: SYSTEM,
    tools,
    messages,
  });

  messages.push({ role: 'assistant', content: response.content });
  if (response.stop_reason !== 'tool_use') break;

  const results: Anthropic.ToolResultBlockParam[] = [];
  for (const block of response.content) {
    if (block.type === 'tool_use') {
      const output = await runTool(block.name, block.input);
      results.push({ type: 'tool_result', tool_use_id: block.id, content: output });
    }
  }
  messages.push({ role: 'user', content: results });
}
```


The SDK can run this loop for you with `client.beta.messages.toolRunner()` and `betaZodTool` from `@anthropic-ai/sdk/helpers/beta/zod`, which also validates each tool's input against a Zod schema. The hand-written version above is worth understanding once, because it is what every agent framework is doing underneath.


**Workflows versus agents.** Both use tools, and the difference is who decides the order. In a workflow, your code controls the steps: call the model to classify, then branch, then call it again to draft, then validate. The model is used at fixed points and never chooses what happens next. In an agent, the model chooses: it looks at the task and the tools and decides which to call, in what order, and when to stop. Workflows are predictable and easy to test. Agents handle open-ended tasks that you cannot script in advance, at the cost of predictability.


## How to choose


### Start with a single call


Classification, extraction, summarising, rewriting, answering from a document you provide: these are one call. Put the data in the prompt, constrain the output with a schema, done. Most LLM features in a typical web app are this.


```typescript
const response = await client.messages.parse({
  model: 'claude-opus-5-5',
  max_tokens: 512,
  output_config: { effort: 'low', format: zodOutputFormat(TicketSummary) },
  messages: [{ role: 'user', content: ticketText }],
});
```


### Use a workflow when you know the steps


If you can draw the flowchart, write the flowchart. Fetch the data in your own code, call the model where judgement is needed, and check each result before moving on. You get logging, retries and tests at every step, and the model only ever sees a focused prompt.


```typescript
const category = await classify(ticket);             // model call 1
const context = await db.findSimilar(category, 5);   // plain code
const draft = await draftReply(ticket, context);     // model call 2
if (!passesPolicy(draft)) await escalate(ticket);    // plain code
```


### Build an agent when the path is unknown


Debugging a failing build, researching a question across several sources, making a multi-file code change: you do not know in advance which files to read or how many searches it will take. Give the model the tools, a clear goal and a stopping condition, and let it work. Keep the tool set small and each tool well described; the model can only be as good as the tools it is offered.


### Know when not to build an agent


Do not build one when a single call or a workflow would do. Do not build one when a wrong answer is expensive and hard to catch, because the model's choices are harder to review than a fixed pipeline's. Do not build one for a task where you cannot describe what "done" looks like, because the loop needs a way to end. And do not build one to look modern. The simplest tier that solves the problem is the right one.


### Budget for cost and latency


Every pass through the loop is a full API call that resends the whole conversation, including every earlier tool result. Ten iterations is not ten times the cost of one call, it is more, because each call is larger than the last. Latency stacks too: a five-step agent is five model round trips plus five tool executions before the user sees anything. Put a hard cap on iterations, log `usage` on every turn, cache the stable system prompt and tool definitions, and stream the final answer so the wait feels shorter.


```typescript
const MAX_TURNS = 12;
let turns = 0;
while (turns++ < MAX_TURNS) {
  // ... the loop from above
}
if (turns > MAX_TURNS) throw new Error('Agent did not finish within budget');
```


## Common pitfalls

- **Building an agent for a one-call problem.** "Summarise this email" needs no loop. Agents cost more, take longer and are harder to debug.
- **No iteration cap.** A model that keeps calling tools without converging will run until your bill or your timeout stops it. Set a limit and handle hitting it.
- **Splitting tool results across messages.** When one turn returns several `tool_use` blocks, send all the results in one user message. Sending them one at a time breaks the conversation shape.
- **Dropping failed tool calls.** If a tool throws, return a `tool_result` describing the error rather than skipping it. The model needs to know, and the conversation needs the matching result.
- **Vague tool descriptions.** The model chooses tools by reading their descriptions. "Gets data" will be misused; "Returns the last 30 days of orders for a customer ID" will not.
- **Forcing a tool call.** `tool_choice` of `any` or a named tool is rejected on current models. Use `auto` and tell the model in the prompt when to use which tool.

## Related reading

- [Tool use overview](https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview)
- [Building effective agents](https://www.anthropic.com/engineering/building-effective-agents)
- [Structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)
