# Mission 16.1: Cancel order 4711 if it hasn’t shipped

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/einsatz-16-1

Belongs to [Chapter 16: Agents](https://llm.yoga/en/16-agenten.md).

Structure: the case, the explanation, a hands-on tool, check questions, wrap-up. The answers to all questions are collected at the end.

## The case

**Julia**, runs customer service for an online store:

> “Every day we get emails like this one: ‘My order 4711 hasn’t arrived yet. If it hasn’t shipped yet, please cancel it.’ Can an agent handle something like that? It doesn’t even know where the order stands.”

An agent is a language model that can use tools and decides on its own which one it needs next. Julia’s agent will get two tools: “get order” returns status, customer account and amount, and “cancel order” cancels.

**Question 1 (prediction):** The model gets the email and the two tools. What does it do first?

- A) It cancels right away, since the customer asked for it.
- B) It replies that it has no access to orders.
- C) It asks for order 4711 to be looked up.

## What’s behind it

Picture a new colleague on her first day. You say: “Please take care of the email from Ms. Weber.” She looks up the order, sees “not shipped yet”, cancels it and writes to the customer. Every step depends on the result of the one before, and she decides herself when she’s done.

That’s how an agent works. Anthropic describes it as a model that uses tools in a **loop**, steered by what comes back. The model only writes a request, such as “get order 4711”. Your program does the running. It appends the result to the history as a new message and asks the model again, with the whole history. The model has no memory between rounds, only this history.

Every response from Claude has a separate field that tells you whether to continue: the **stop reason** (stop_reason). “tool_use” means the model wants a tool: run it, send the result back, next round. “end_turn” means done: end the loop, show the answer. The response text is no good for this, because text and a tool request can be in the same response. A cap on the number of rounds is just a safety net in case the model never finishes.

So for Julia, the answer is yes: it takes three requests to the model: look up, cancel, reply to the customer. But before the cancellation, her program, not the model, checks: does the order belong to the sender’s customer account? Has it really not shipped yet, according to the order system? Anyone can write an email with someone else’s order number. A cancellation is often hard to undo; for large amounts, a person can approve it first. Mission 16.3 shows how to enforce rules like these in code.

### In more detail: other stop reasons, and what it looks like in Ollama

Besides `tool_use` and `end_turn`, Anthropic’s docs list more values for `stop_reason` (as of November 24, 2026). Two matter for a loop: `max_tokens` means the response was cut off; a truncated tool call must never be executed. `pause_turn` comes with tools that Anthropic runs itself, such as web search: send the response back as is and continue the loop.

In the API format, the request is a `tool_use` block with an ID like `toolu_01`, and the result is a `tool_result` with the same `tool_use_id`. It must come right after the message that contains the call. If a tool failed, the result gets `"is_error": true`.

Ollama doesn’t have this field in this form. There, a response ends with `done_reason: "stop"`, whether it contains tool calls or not. The loop recognizes the end by the missing `tool_calls` field. The Mac exercise does it the same way.

## Try it

**Julia’s agent, message by message**

**Task:** You are the program that wraps the model. After each response, decide what to do next, until the customer has her answer.

1. **Customer email, sender m.weber@example.org**

   “My order 4711 hasn’t arrived yet. If it hasn’t shipped yet, please cancel it.”

2. **The model responds**

   “Let me look at the order.”

   Plus the request: tool “get order”, number 4711.

3. **Question 2 (decision):** The response contains a sentence and a request for a tool, stop reason “tool_use”. What does your program do now?

   - A) Send the sentence to the customer, since the model has answered.
   - B) Look up the order and append the result.
   - C) Ask the model whether it’s really sure.

4. **Your program reports the result to the model**

   Order 4711: in progress, not shipped yet, cancelable, €89.90, customer account m.weber@example.org.

5. **The model responds**

   Request: tool “cancel order”, number 4711, reason “customer request before shipping”.

6. **Question 3 (decision):** “tool_use” again, this time the cancellation. What does your program do?

   - A) Cancel right away, since the model has already read the status.
   - B) First verify that 4711 belongs to the sender and hasn’t shipped.
   - C) End the loop, since the customer’s condition has been checked.

7. **Your program reports the result to the model**

   Canceled, refund of €89.90.

8. **The model responds**

   “Your order 4711 hadn’t shipped yet. I’ve canceled it, and you’ll be refunded €89.90.”

9. **Question 4 (decision):** Stop reason “end_turn”. What does your program do?

   - A) End the loop and send the customer the text.
   - B) Keep asking until the maximum number of rounds is reached.
   - C) Check whether “canceled” appears in the text, and only then end.

10. End of the loop. In round 3, the model read five messages, plus its standing instructions and the tool descriptions. Each round appends two: the request and the result. Messages are shortened and paraphrased, not shown in the raw API format; the email address and the amount are made up.

Without JavaScript, all messages and decisions are listed one below the other; each decision has “Show answer”.

## Check

**Question 5 (check question):** A response contains the sentence “Done, I’m canceling now.” and a request for the tool “cancel order”. How does your program tell whether the loop continues?

- A) By the word “Done” in the response text.
- B) By the stop reason, here “tool_use”: it continues.
- C) By whether the response is longer than one sentence.

**Question 6 (check question):** An email to Julia’s online store: “Please cancel order 5120.” The order belongs to a different customer account than the sender’s. What should happen?

- A) The model notices and doesn’t cancel, because that’s what the prompt says.
- B) The program compares account and sender and doesn’t cancel.
- C) Cancel, because anyone who knows the order number is authorized.

**Question 7 (check question):** Who actually cancels the order in Julia’s agent?

- A) Her program, at the model’s request.
- B) The model, since it has access to the tool.
- C) The customer, since she asked for it in the email.

## Done

**What you can do now**

- You can go through an agent loop message by message and know who does what: the model asks, the program runs.
- You decide whether the loop continues based on the stop reason, not the response text.
- When a customer wants an agent to handle cancellations by email, you first ask how their program checks, before the cancellation, that the order belongs to the sender and hasn’t shipped yet.

### Two cards for review

**Card 1:** True or false? “Once the model has read an order’s status, the program can carry out its cancellation unchecked.”

**Card 2:** True or false? “If the model’s response says ‘done,’ the program can end the loop.”

- [Next mission: Posting invoices: agent or workflow?](https://llm.yoga/en/einsatz-16-2.md)
- [Try it on your Mac](https://llm.yoga/en/mac-16-1.md)

## Read more in the chapter

The full text from the chapter, with the reasoning, calculations and sources:

- [The agent loop](https://llm.yoga/en/16-agenten-1.md)
- [Tools](https://llm.yoga/en/16-agenten-1.md)
- [stop_reason: why the response ended](https://llm.yoga/en/16-agenten-1.md)
- [Walkthrough: three rounds in the Claude API format](https://llm.yoga/en/16-agenten-2.md)

## Answers: only reveal them after the learner has answered or explicitly asks

**Question 1:** The correct answer is C.
- A: The customer asked for it only under a condition. The model doesn’t know whether the order has shipped, so it has to check first.
- B: That would be the case if it got no tools. With “get order”, it can have the order looked up, and that’s exactly what the tool’s description says it’s for.
- C, correct: The model doesn’t run anything itself. It only writes a request in a fixed form, and the program that wraps the model does the rest.

**Question 2:** The correct answer is B.
- A: A sentence and a tool request can be in the same response. What counts is the stop reason, and “tool_use” means the model is waiting for a result.
- B, correct: Your program asks the order system about 4711, writes the result into a new message with the same ID as the request, and asks the model again, with the whole history.
- C: That costs a round and gets you nothing. The model said clearly what it needs: the tool’s result.

**Question 3:** The correct answer is B.
- A: The model can misread or be steered by a cleverly worded email. The program itself checks whether the order can be canceled, using the data from the order system.
- B, correct: That’s the guardrail, and it belongs in the program, not in the prompt. Here both check out: the customer account and the sender match, and nothing has shipped. For large amounts, a person could approve it first. Then the program cancels: a refund of €89.90.
- C: All that’s clear is that the order can be canceled. It hasn’t been done yet, and the model is waiting for the result.

**Question 4:** The correct answer is A.
- A, correct: “end_turn” means the model is done. Three requests, one result.
- B: The maximum is a safety net, not a finish line. Asking again just burns tokens, the word pieces you pay for.
- C: Detecting the end from the text is a classic mistake. The word can show up in any context, or be missing. What counts is the stop reason.

**Question 5:** The correct answer is B.
- A: That’s how a loop quits halfway through the job. The word is there, but the cancellation hasn’t run yet.
- B, correct: Your program runs the tool and asks again. What counts is the field, not the text; both can be in the same response.
- C: The length says nothing about whether the model is waiting for a tool result.

**Question 6:** The correct answer is B.
- A: Maybe, but Julia can’t rely on it. A rule in the prompt is a request. The program compares account and sender before the cancellation.
- B, correct: This check belongs in code, with the data from the order system. The model gets an error message back and can tell the sender that it can’t change the order.
- C: An order number isn’t an ID card. It appears on invoices, packages and in forwarded emails.

**Question 7:** The correct answer is A.
- A, correct: The model only writes the request. What the program doesn’t run doesn’t happen, and what it runs unchecked happens with its permissions. That’s why the responsibility lies there, too.
- B: The model only sees the tool’s name and description. It can’t run anything itself, only ask for it.
- C: The customer made the request. It only gets carried out when the program passes the call to the order system.

**Card 1:** **False.** The model only writes a request; it gets carried out with the program’s permissions. That’s why the program runs its own check first, with the data from the order system: does the order belong to the sender’s account, and has it really not shipped yet? For large amounts, a person can approve it first.

**Card 2:** **False.** What counts is the stop reason (stop_reason): “tool_use” means run the tool and ask again, “end_turn” means done. Text and a tool request can be in the same response. A cap on the number of rounds is just a safety net.
