Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

The agent that said "let me look that up" and never did

The agent that said "let me look that up" and never did A support agent at a made-up outdoor gear shop handled 2,100 chats over one weekend. By Monday, 340 of them, about 16 percent, ended the same way: the agent typed

The agent that said "let me look that up" and never did

A support agent at a made-up outdoor gear shop handled 2,100 chats over one weekend. By Monday, 340 of them, about 16 percent, ended the same way: the agent typed "Let me look up your order for you." Then nothing. No lookup. No answer. The customer wrote back, confused, and the chat started over.

Each of those dead ends cost about $6 in repeat contact time. That is 340 times $6, or $2,040, spent on a weekend of conversations that went nowhere. First-contact resolution landed near 64 percent against a goal of 80. The agent was not lazy and the model was not broken. The loop that ran it was.

The loop is a kitchen, not a brain

An agentic loop is only a few lines of your code. Think of a kitchen ticket. Claude is the cook, your code is the runner, and the conversation history is the counter where the ticket sits. The cook calls out for salt, the runner fetches it and puts it on the counter, the cook reads the ticket again and calls out the next thing. One trip around the loop goes like this:

  1. The customer writes, "Refund my trail jacket, order 48213." That becomes the first message on the ticket.
  2. Your code sends the system prompt, the tool definitions and the full history to Claude. Every request stands alone, so the history is Claude's whole memory.
  3. The reply comes back with content blocks and a field called stop_reason. In this case the reply holds a friendly sentence and a request to call get_customer, and the reason is "tool_use".
  4. Your code runs the real function. Claude only asked for it, it never runs anything itself.
  5. Your code appends Claude's whole reply to the history, then adds one user message carrying the tool results.
  6. Repeat. lookup_order, then process_refund. Each trip re-sends everything that came before.
  7. The last reply carries "end_turn" and the words, "Your $129.00 refund is on its way." Stop, and hand that answer to the customer.

That last step is where Pine, the shop's agent, failed. The loop stopped at the first piece of text it saw, and Claude often writes a friendly sentence beside its tool request. The sentence said "let me look that up." The stop_reason field said "tool_use", which means keep going. The code read the sentence and quit.

The field is the only signal you can trust

stop_reason has only a few values that matter here. "tool_use" means Claude wants tools run, so keep going. "end_turn" means Claude finished, so stop. Other values exist, such as "max_tokens", and the honest move with an unfamiliar one is to log it and fail loudly rather than guess.

The history follows a strict pattern, and getting it wrong is a second quiet way to break the loop. A user message opens with the question. An assistant message carries Claude's full reply, text and tool requests together, each request with its own id. Then a user message carries one result per request, each naming the tool_use_id it answers. Append the whole assistant reply, not just its text. Two tool requests in one reply means both results go in the same user message. Miss that pairing and the model loses track of which result belongs to which request.

Three stop rules that fail

The wrong stop rules look sensible until they cost you a weekend of chats.

First, reading the text for a phrase like "I'm done." Wording changes. The text is written for the customer. The field is written for your code.

Second, a turn cap as the main stop rule. A loop that stops after five turns cuts off a six-tool dispute, possibly after process_refund ran but before the customer was told. Keep a generous cap as a backstop only, something like 25 turns, logged, with anything past it handed to a human.

Third, treating any text as completion. Text often sits beside a tool request, and that was exactly the cold-open bug. One rule beats all three: read why the API says it stopped.

Who picks the next step

There are two ways to run the loop. Model-driven means you list the tools and Claude picks the next one, or decides it is done. Pre-configured means you fix the order in code: get_customer, then lookup_order, then process_refund. A fixed sequence suits narrow, strict jobs, but a chat mixing a dispute, a return and a wrong address forces you to code every path. Pine's requests are open-ended, so model-driven fits. Rules that must always hold, like "refunds above $500 need a human", still belong in code, not in a prompt.

What one refund costs

A four-turn refund is cheap, but the arithmetic shows where the money actually goes. At made-up prices of $3 per million input tokens and $15 per million output tokens, the refund takes four calls. The first sends 1,560 input tokens. The second re-sends everything, 1,840. The third, 3,730, because the order record alone is 1,800 tokens, 40 fields when only 5 matter. The fourth, 3,915. Total: 11,045 input tokens and 325 output tokens, about $0.038. The driver is that fat order record re-sent on every later call. Return only the five fields that matter and the cost drops with the noise.

That is the same lesson at both ends of the loop. The small, boring details are the system. The stop signal is a field, not a sentence. The history is a strict pattern, not a pile of messages. The cost driver is a record nobody trimmed. All of it is the difference between an agent that finishes the job and one that says "let me look that up" and never does.

The full interactive chapter is free at https://deployedlab.dev/lessons/cca-03-agentic-loop, with a hands-on lab where you fix the broken loop Pine shipped. 79 chapters are free with no signup, and the full bundle is $5 a month or $39 a year at deployedlab.dev.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.