🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

The Locked Box

Explore the mechanics of Tool Use and Function Calling. Learn the multi-step API flow required to execute code on behalf of an LLM, and discover how placing tools inside a loop creates an autonomous Agent.

Total XP: 0|💻 generativeai XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

The Locked Box

Production details.

Quick Quiz //

When you provide a 'tool' to an LLM via the API, does the LLM actually execute the Python or JavaScript code inside the tool?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

Let's cut the fluff. Here is exactly what you need to know about this concept to survive in a real production AI environment.

1The Locked Box

Look, if you've ever dealt with this in production, you know exactly what the problem is. LLMs exist in a locked box. They cannot execute code, they cannot browse the live internet, and they cannot check the weather. If you ask an LLM 'What is the temperature in New York right now?', its autoregressive engine will simply hallucinate a number because it has no physical connection to live APIs. To fix this, we must give the LLM 'Tools'. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# The Locked Box Limitation

Prompt: "What is the weather in NYC today?"

# AI tries to guess based on training data:
AI: "The weather in NYC is 72 degrees and sunny."
# FACT CHECK: It is actually snowing.
localhost:3000
AI Execution Environment
[The Locked Box] Output:

Model execution completed successfully. Inference generated valid results.

2Defining the Tool

Look, if you've ever dealt with this in production, you know exactly what the problem is. To give an LLM a tool, you must define it in the System Prompt using a strict JSON schema. You tell the LLM: 'You have access to a tool called get_weather. It requires a parameter called location. If you need the weather, do not guess. Output a JSON object requesting to use this tool.' You are teaching the model the syntax of your backend API. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# Defining a Tool for the LLM

tools = [{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "Get current weather",
    "parameters": {
      "type": "object",
      "properties": {
        "location": {"type": "string"}
      }
    }
  }
}]
localhost:3000
AI Execution Environment
[Defining the Tool] Output:

Model execution completed successfully. Inference generated valid results.

3The Function Call

Look, if you've ever dealt with this in production, you know exactly what the problem is. When the user asks 'What is the weather in NYC?', the LLM reads the tool schema you provided. Its Attention mechanism realizes that it cannot answer the question directly, but the get_weather tool can. Instead of outputting a conversational reply, the API returns a 'Function Call'—a perfectly formatted JSON object containing the arguments needed to run the tool. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# The API Returns a Function Call

response = llm.generate("Weather in NYC?", tools=tools)

# The LLM does NOT say "Here is the weather"
# It outputs a request for YOU to run code:
print(response.tool_calls)
# [{"name": "get_weather", "arguments": "{\"location\": \"NYC\"}"}]
localhost:3000
AI Execution Environment
[The Function Call] Output:

Model execution completed successfully. Inference generated valid results.

4Execution and Return

Look, if you've ever dealt with this in production, you know exactly what the problem is. This is where your code takes over. Your Node.js or Python backend detects the tool_call from the LLM. Your backend executes the actual get_weather('NYC') function against a real weather API. Your backend gets the result (e.g., '32 Degrees'). You then append this result to the message array as a 'Tool' role, and send the array *back* to the LLM. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# Backend Execution

# 1. We run the function locally
result = actually_get_weather("NYC") # Returns "32F"

# 2. Append result to message history
messages.append({
  "role": "tool", 
  "name": "get_weather", 
  "content": "32F"
})

# 3. Call LLM again!
localhost:3000
AI Execution Environment
[Execution and Return] Output:

Model execution completed successfully. Inference generated valid results.

5The Final Synthesis

Look, if you've ever dealt with this in production, you know exactly what the problem is. On the second API call, the LLM reads the entire history: The user's question, its own function call, and the raw data result provided by your backend. It synthesizes this raw data into a natural, conversational response. The user sees: 'It is currently 32 degrees in NYC, you should bring a coat!', completely unaware of the complex multi-step loop that just occurred. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# The Second LLM Call

# LLM reads the tool result ("32F")
final_response = llm.generate(messages)

# AI Synthesis:
print(final_response)
# "It's quite cold in NYC today at 32°F!"
localhost:3000
AI Execution Environment
[The Final Synthesis] Output:

Model execution completed successfully. Inference generated valid results.

6The Birth of Agents

Look, if you've ever dealt with this in production, you know exactly what the problem is. When you give an LLM multiple tools (Search Web, Read File, Execute Python) and put it inside a while loop, you have created an 'Agent'. The LLM can formulate a plan, call a tool, read the result, realize it made a mistake, call a different tool, and keep looping until the task is complete. This transforms the LLM from a text generator into an autonomous software worker. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
# The Agent Loop

while task_not_complete:
    action = llm.decide(context)
    if action.is_tool():
        result = execute(action)
        context.append(result)
    else:
        return action.final_answer()
localhost:3000
AI Execution Environment
[The Birth of Agents] Output:

Model execution completed successfully. Inference generated valid results.

7Mastery Achieved

Look, if you've ever dealt with this in production, you know exactly what the problem is. You have completed the Generative AI Masterclass! From the probabilistic math of Next-Token Prediction and the geometry of Embeddings, to Advanced Prompting, RAG, Fine-Tuning, and Autonomous Agents. You are no longer just chatting with AI; you are engineering it. The digital world is now yours to command. This isn't just academic theory—understanding the *why* behind this is what separates junior devs from senior AI engineers. When you deploy models to a cluster, this is the mechanic that prevents catastrophic failure.

+
/* Course Complete */
.curriculum { status: 'expert'; }
localhost:3000
AI Execution Environment
[Mastery Achieved] Output:

Model execution completed successfully. Inference generated valid results.

8Step-by-Step Breakdown

The Locked Box. LLMs exist in a locked box. They cannot execute code, they cannot browse the live internet, and they cannot check the weather. If you ask an LLM 'What is the temperature in New York right now?', its autoregressive engine will simply hallucinate a number because it has no physical connection to live APIs. To fix this, we must give the LLM 'Tools'.

Defining the Tool. To give an LLM a tool, you must define it in the System Prompt using a strict JSON schema. You tell the LLM: 'You have access to a tool called get_weather. It requires a parameter called location. If you need the weather, do not guess. Output a JSON object requesting to use this tool.' You are teaching the model the syntax of your backend API.

When you provide a 'tool' to an LLM via the API, does the LLM actually execute the Python or JavaScript code inside the tool?

  • No. The LLM only generates a text string (JSON) requesting that the tool be called. Your backend server is what actually executes the code.
  • Yes. The LLM runs the code internally.

The Function Call. When the user asks 'What is the weather in NYC?', the LLM reads the tool schema you provided. Its Attention mechanism realizes that it cannot answer the question directly, but the get_weather tool can. Instead of outputting a conversational reply, the API returns a 'Function Call'—a perfectly formatted JSON object containing the arguments needed to run the tool.

Execution and Return. This is where your code takes over. Your Node.js or Python backend detects the tool_call from the LLM. Your backend executes the actual get_weather('NYC') function against a real weather API. Your backend gets the result (e.g., '32 Degrees'). You then append this result to the message array as a 'Tool' role, and send the array *back* to the LLM.

In the Function Calling workflow, how many times does your application make a request to the LLM API to answer a single user question that requires a tool?

  • Twice. Call 1: LLM decides to use the tool and returns the JSON. Call 2: You send the tool's result back to the LLM so it can write the final conversational answer.
  • Once. The LLM handles everything internally in a single pass.

The Final Synthesis. On the second API call, the LLM reads the entire history: The user's question, its own function call, and the raw data result provided by your backend. It synthesizes this raw data into a natural, conversational response. The user sees: 'It is currently 32 degrees in NYC, you should bring a coat!', completely unaware of the complex multi-step loop that just occurred.

The Birth of Agents. When you give an LLM multiple tools (Search Web, Read File, Execute Python) and put it inside a while loop, you have created an 'Agent'. The LLM can formulate a plan, call a tool, read the result, realize it made a mistake, call a different tool, and keep looping until the task is complete. This transforms the LLM from a text generator into an autonomous software worker.

What transforms a standard LLM chatbot into an 'Agent'?

  • Giving the LLM access to external Tools and placing it in an execution Loop, allowing it to take autonomous actions and observe the results until a goal is met.
  • Giving the LLM a massive Context Window.

Trigger a Real Function Call. This is the exact get_weather tool schema from this lesson, sent to a real model through the OpenAI tools API. Run it and check the response: instead of a plain text answer, you should see a structured tool_calls block requesting get_weather with location: "NYC" — proof the model routes to your backend instead of guessing the weather itself.

Mastery Achieved. You have completed the Generative AI Masterclass — and just watched a real model halt text generation to request a structured function call instead of guessing an answer. From the probabilistic math of Next-Token Prediction and the geometry of Embeddings, to Advanced Prompting, RAG, Fine-Tuning, and Autonomous Agents. You are no longer just chatting with AI; you are engineering it. The digital world is now yours to command.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Semantic Usage

Using the proper structure for The Locked Box ensures that screen readers can correctly interpret the content hierarchy and purpose.

<!-- Apply semantic elements appropriately -->

SEO Implications

  • 1

    Contextual Relevance

    Proper implementation of The Locked Box provides search engine crawlers with better context, improving the indexing accuracy of your page.

Best Practices

Clean Code

Always validate your structure when using The Locked Box to prevent layout shifts and DOM inconsistencies.

Separation of Concerns

Keep styling and behavior separate from the structural markup of The Locked Box.

Frequent Bugs

THE BUG

Unexpected layout shifts or styling failures.

THE FIX

Ensure all implementations related to The Locked Box are properly structured according to strict specifications.

Real-World Examples

Production Usage

Here is how The Locked Box is typically implemented in a professional, robust application.

<!-- Best practice implementation of The Locked Box -->
<div class="production-ready">
  <!-- Content -->
</div>

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Not reading error messages carefully

Uncaught TypeError: Cannot read properties of undefined (reading 'length') // Solution: Ensure the variable you are calling .length on is initialized as a string or an array, not undefined.

The Solution //

Most of the time, the compiler or interpreter tells you exactly what line caused the crash and why. Read stack traces from the top down to identify the root cause.

The Error //

Hardcoding sensitive credentials

// Wrong const API_KEY = 'sk-123456789'; // Correct const API_KEY = process.env.API_KEY;

The Solution //

Never hardcode API keys, passwords, or secrets in your source code. Use environment variables (.env files) to keep them secure and out of version control.

Lesson Glossary

[01]Function Calling

The capability of an LLM to output a structured JSON request to execute a predefined tool, rather than conversational text.

Code Preview
The Tool

[02]Agent

An LLM-driven system placed in an execution loop, capable of planning, using tools, and course-correcting to achieve a goal.

Code Preview
The Worker

[03]ReAct Framework

Reason, Act, Observe. A popular prompting framework that teaches an Agent how to deliberate before calling a tool.

Code Preview
The Loop

Continue Learning