Lesson 19 / 25

Forcing a Final Answer

Use a last call with no tools to summarise progress when the budget runs out.

A last call that cannot use tools

When a limit is reached mid-task, do not just raise an error. Reserve a small budget for one wrap-up call with tools disabled, and ask the model to summarise what it did, what it found and what remains. The user gets something useful instead of silence.

Stop with something useful

When a limit is hit, the best loops still return a partial result, save state and say what happened.

Three parts of a good ending: result, state, explanation.
Figure 6.1 — Partial result, saved state and explanation.

The wrap-up call

Omitting tools means the model can only write text. Keep the wrap-up request short and its max_tokens modest.

def wrap_up(messages, reason):
    messages.append({"role": "user", "content":
        f"Stopping because: {reason}. Do not call tools. Summarise what you "
        "completed, what you found, and what still needs to be done."})
    reply = client.messages.create(model=MODEL, max_tokens=800, messages=messages)
    return final_text(reply)

Reserve the budget first

Stop the main loop slightly before the hard limit (say at 90%) so the wrap-up call has room to run. A wrap-up that itself exceeds the budget defeats its purpose.

Quick check: Why disable tools in the wrap-up call?

  • So the model writes a summary instead of starting more work
  • Tools cannot be used twice
  • It makes the call free
  • Tools break summaries
Answer

So the model writes a summary instead of starting more work — Without tools the only possible output is the final text.