Tools · Free preview

Closing the Loop

Tool result becomes the answer

An agent's final answer must be built from the value the tool actually returned, not from a guess made before the tool ran.

Closing the loop

You've routed to the right tool and called it — but a tool call is only half the job, and the missing half is where agents quietly break. Picture it: the model decides to call get_weather("Paris"), the runtime runs it and gets back 14C… and then the model writes "The current temperature in Paris is 20C." It ignored its own tool. The call succeeded and the answer is still a hallucination, because the model answered from the guess it had already half-formed instead of from what came back.

That last step is the observe step, and it's what closes the loop: call → result → fold the result into the answer. The intuition is that the returned value must become the source of the sentence, not a decoration next to it. In code, that's the difference between a hard-coded string and one built by interpolating the returned value into the sentence. The moment the answer is derived from toolResult, it can't drift: change the tool's output to 9C and the sentence follows automatically.

This is why it matters in real agents. A model that talks over its tools gives you the worst of both worlds — you paid for the call and still shipped a wrong answer, and now it looks grounded, so nobody catches it. Trust in an agent comes from the answer provably tracking the tool, every time.

Here the runtime has already called get_weather("Paris") and handed you toolResult. Build the final answer from it — interpolate the value — so the line reads exactly The current temperature in Paris is 14C. Done is that sentence reflecting the real returned value with no number you typed yourself.

The loop closes when the tool's real output becomes the answer. Anything else is the model talking over its own tools.

In the full academy, you write and run this — live, graded:

// The runtime already called the tool for you and handed back its result.
const toolResult = getWeather("Paris"); // returns "14C" — the REAL observed value
function getWeather(city) { return "14C"; }

console.log(`tool get_weather(${"Paris"}) -> ${toolResult}`);

// TODO: build the final answer FROM toolResult — don't guess the temperature.
// It must read exactly: The current temperature in Paris is 14C.
const answer = "The current temperature in Paris is 20C.";

console.log(answer);

🔒 Live code execution, real agent runs, mastery tracking and verifiable credentials unlock with the full academy.

This is 1 of 50 lessons.

The full academy: write real code, watch real agents run, and earn verifiable credentials — across 8 tracks, in a 3D campus.

Unlock the full academy — $100 →

14-day refund · 🔒 Stripe-secured checkout · lifetime access

More free lessons: An LLM Is a Function  ·  The Agent Loop  ·  Define a Tool  ·  Give an Agent a Tool  ·  Durable State

← The Agent Marketplace