Skip to main content
Lesson 9: Tool Integration This lesson expands on what “tools” are for LLMs and demonstrates how they work in practice through the tool-use loop. Integrating tools lets an LLM do more than generate text — it can request actions, receive results, and continue reasoning based on real-world data.
A dark, retro-style infographic titled "THE TOOL-USE LOOP" showing two pixelated boxes labeled "LLM" and "YOUR CODE" connected by an arcing line labeled "Tool Call." A small subtitle reads "How LLMs interact with tools in a continuous cycle," with faint text at the bottom saying "Or just reply with text."

What is the tool-use loop?

When you grant an LLM access to tools, the model can either:
  • reply with plain text, or
  • output a structured tool call (often JSON) expressing an intent.
If the model requests a tool, your application must execute that action and return the result to the model. This back-and-forth creates the tool-use loop (a.k.a. function calling), where the LLM delegates work to your application and then continues reasoning with the returned outputs. Key benefits:
  • Lets the model perform concrete actions (e.g., read a calendar, call an API, query a database).
  • Keeps the model’s reasoning connected to external state and real-time data.
  • Enables multi-step workflows that combine model planning with programmatic execution.

Typical sequence (example: scheduling a meeting)

A single user request may lead to several tool calls in sequence. For example: “Schedule lunch with Sarah tomorrow at noon” might produce the following steps:
  • check_calendar → verify availability
  • search_contact → find Sarah’s contact info
  • create_event → schedule the meeting
Example flow (human-readable): “Schedule lunch with Sarah tomorrow at noon”
  1. check_calendar
    date: “2026-03-06”
    → 12:00–1:00 PM is free
  2. search_contact
    name: “Sarah”
    → sarah@email.com
  3. create_event
    title: “Lunch with Sarah”
    → Event created
Sequential: 1 → 2 → 3

Implementation pattern

In practice you maintain a messages list (the conversation), send it with tool definitions to the LLM, detect returned tool calls, execute them in your application, and append the results back into messages so the model can continue. Example Python-style workflow:
Common tool-call payloads resemble function calls represented as JSON. Example:
The LLM outputs intent (e.g., a JSON function call). Your application acts as the middleman: run the requested action, return the result as a tool message, and continue the conversation so the model can finalize the response.

Tool-use loop lifecycle (quick reference)

Best practices

  • Define clear tool interfaces (names, arguments, expected outputs) so the model’s tool calls are predictable.
  • Validate tool arguments before executing to avoid unintended side effects.
  • Return structured, machine-readable tool results that the model can consume reliably.
  • Keep the messages history complete: include user input, model tool calls, tool outputs, and any follow-up model messages.
  • Consider idempotency and retries for tools that perform side-effecting operations (e.g., creating calendar events).
Further reading and references: Tool integration transforms a text generator into an action-capable agent by coordinating model reasoning with your application’s execution logic. Use the tool-use loop to build robust multi-step workflows that combine the strengths of LLMs with deterministic programmatic actions.

Watch Video

Practice Lab