systemdesign.academyAI EngineeringFree lessonsSign inGet the course
  1. Home
Previous lessonThe Agent Loop: Why AI Agents Fail as Tasks Get LongerNext lessonDurable Execution: An Agent Run Is a Workflow, Not a Request

System Design

  • Foundation
  • Intermediate
  • Advanced
  • Capstone

AI Engineering

  • Foundation
  • Data, RAG and Agents
  • Evaluation, LLM Ops and Security

systemdesign.academy

  • Home
  • Glossary
  • Interview prep
  • Reviews
  • About
  • Pricing
  • Privacy
  • Terms
  • Refunds
  • Delivery
  • Contact
AI Engineering: Data, RAG and Agents
  • Agents in Production
  • Tool Calling Is a Contract, and the Model Will Break It
  • AI Engineering · Agents In Production

    Tool Calling Is a Contract, and the Model Will Break It

    Free preview: the first 2 parts of this 15-part lesson.

    Already bought the course? Sign in to read the whole lesson.

    this lesson is part of

    AI Engineering

    Unlock this lesson and the 199 other paid lessons

    • 210 lessons in 16 chapters
    • 118 lessons built on a lab that really ran
    • An AI tutor beside every lesson that answers and draws
    • Lifetime access, every new lesson included

    $49 once, by PayPal

    See the AI Engineering course

    The rest of this lesson

    1. What a Tool Call Is Made Of
    2. Why Does a Good Model Emit a Bad Call?
    3. Validate First, Execute Second, Reject Cleanly
    4. How Do You Make a Tool Contract Enforceable?
    5. Watch a Bad Call Get Rejected, Then Corrected
    6. The Gate Code, Explained Part by Part
    7. Fewer, Sharper Tools Get Chosen Correctly
    8. A Tool Result Is Untrusted Input, Not an Answer
    9. Does MCP Replace Tool-Call Validation?
    10. Common Mistakes
    11. How Strict to Be, and When
    12. What to Hold Onto
    13. Knowledge Check

    read the rest of this lesson in

    AI Engineering

    Unlock this lesson and the 199 other paid lessons

    • 210 lessons in 16 chapters
    • 118 lessons built on a lab that really ran
    • An AI tutor beside every lesson that answers and draws
    • Lifetime access, every new lesson included

    $49 once, by PayPal

    See the AI Engineering course

    Continue this lesson

    in Agents In Production

    This lesson is part of the AI Engineering course. Unlock the full course, a separate one-time payment, for lifetime access.

    One-time payment. Lifetime access. No subscription.
    Global
    $49/lifetime

    One-time payment via PayPal

    Get Lifetime Access
    Foundation lessons free, try before you buy
    India Special
    India
    ₹999/lifetime

    One-time payment: UPI, card or netbanking

    Get Lifetime Access
    Foundation lessons free, try before you buy
    Every ML Platform lesson, Foundation to Advanced
    Real architecture diagrams with real tool logos
    MLOps, LLMOps, and data engineering in depth
    Quizzes with instant feedback
    Built and reviewed by senior engineers
    Lifetime access, all future content included

    Foundation lessons of this course are free, no payment needed. Try them now

    The Model Never Actually Calls Your Function

    Here is the sentence that causes most bugs: "the model called the weather API." It did not. It cannot. A language model produces one thing, text, and it has no way to reach out and run your code.

    What actually happens is smaller and stranger. You describe your tools to the model. When it decides it wants one, it writes text that names the tool and gives a JSON object of arguments. That text is a request, a note that says "please run get_weather with {"city": "Berlin"}." Your own code, the orchestrator from the previous lesson, reads that note, runs the real function, and writes the result back into the model's context. The model proposes. The orchestrator disposes.

    That gap is the whole lesson. Because the model's request is just text it generated, it can be wrong. It can pass a string where you need a number. It can leave out a required field. It can name a tool that does not exist. It can pick a real tool that is the wrong one for the job. None of these are exotic. They happen in normal operation, from good models, on ordinary inputs. If your orchestrator runs whatever the model proposes without checking it, those broken proposals hit your real functions, and your database write, your payment call, your file delete, runs with garbage arguments.

    So tool calling is a contract. The schema is the contract. The model is a party that will, some fraction of the time, break it. Your job is to be the party that checks the contract on every single call before anything real happens.

    The Words You Need First

    A few terms appear on almost every slide of this lesson. Here they are in plain English.

    JSON is a plain text format for data. It writes values as text, for example {"city": "Berlin", "days": 3}. The part before each colon is a field name, and the part after it is the value. Notice that 3 without quotes is a number, and "3" with quotes is a piece of text, called a string. That small difference causes many real bugs.

    The arguments of a tool call are the inputs the model wants to pass to the tool, written as a JSON object.

    A schema is a written description of what valid arguments look like: which fields must be there, what type each one has, and which values are allowed. JSON Schema is a common standard format for writing such descriptions, and it is the format model providers accept when you describe a tool.

    A type is the kind of a value: a whole number (an integer), a decimal number, a string, true or false (a boolean), a list or an object.

    An enum is a field that may only take one value from a fixed list, for example "public" or "private".

    To validate means to check the arguments against the schema and collect every rule they break, before anything runs.

    An observation is what the orchestrator writes back into the model's context after a call: the tool's result if it ran, or an error message if it did not. The model reads the observation on its next turn.