Implementing Function Calling and Tool Use in AI Agents: A Complete Guide to the ai-engineering-from-scratch Curriculum

The ai-engineering-from-scratch repository provides a phase-driven curriculum that teaches function calling through a four-step loop—tool definition, model parsing, execution, and result injection—using a modular architecture that abstracts provider-specific APIs into a unified transport layer.

This comprehensive open-source curriculum walks learners from first principles to production-grade AI agents, with implementing function calling and tool use in AI agents serving as a core competency developed across Phases 13 and 14. The repository treats function calling as a standardized transport protocol, enabling seamless portability across OpenAI, Anthropic, Gemini, and custom MCP servers.

The Four-Step Function Calling Loop

According to the source code in phases/13-tools-and-protocols/01-the-tool-interface/docs/en.md, the curriculum codifies function calling into a reproducible execution cycle that bridges LLM token generation with host-side actions.

1. Tool Interface Definition

The process begins with a JSON-Schema contract that declares available tools, their parameters, and type constraints. This schema lives in lesson documentation (e.g., phases/13-tools-and-protocols/02-function-calling-deep-dive/docs/en.md) and provides the LLM with structured metadata about what functions it can invoke and how arguments must be formatted.

2. Model Output Parsing

When the LLM decides to invoke a tool, it returns provider-specific payloads—function_call for OpenAI, tool_use for Anthropic, or functionDeclarations for Gemini. The curriculum's Tool Registry pattern (documented in outputs/skill-function-calling-patterns.md) normalizes these disparate formats into a unified internal representation.

3. Tool Execution

The host program looks up the requested tool in the registry, validates incoming arguments against the JSON schema, and executes the implementation. As shown in phases/13-tools-and-protocols/02-function-calling-deep-dive/code/main.py, this step includes exception handling to capture runtime errors for safe return to the model.

4. Result Injection

The tool's output is serialized and re-inserted into the conversation context, allowing the LLM to continue reasoning with fresh data. This closes the loop and enables multi-turn tool use workflows essential for complex agentic tasks.

Repository Architecture for Tool Use

The repository organizes its function calling curriculum into five distinct architectural layers, as defined in the root AGENTS.md contract:

  • Curriculum Core: Lessons are grouped into phases, with tool-specific content concentrated in phases/13-tools-and-protocols/ and phases/14-agent-engineering/
  • Lesson Assets: Each lesson contains docs/en.md for theory, code/main.* for implementations, and code/tests/ for validation
  • Reusable Skills: Centralized artifacts in outputs/skills/ and outputs/agents/ store tool definitions and MCP server configurations that lessons emit for production use
  • Build Pipeline: site/build.js and scripts/build_catalog.py transform the markdown curriculum into searchable documentation and e-books
  • Project Metadata: ROADMAP.md and glossary/terms.md standardize terminology across the function calling implementations

Practical Implementation Examples

Scaffolding a New Lesson

The repository includes automation scripts to standardize lesson creation. To scaffold a new function calling lesson under Phase 13:

scripts/scaffold-lesson.sh 13 tool-use-and-function-calling my-new-lesson

This generates the required directory structure (docs/, code/, code/tests/, quiz.json) with template content adhering to the AGENTS.md conventions.

Defining Tool Interfaces in Python

The following implementation from phases/13-tools-and-protocols/02-function-calling-deep-dive/code/main.py demonstrates a simple tool with proper JSON serialization:

"""Tool: web_search – fetches the first result for a query."""
import json
import urllib.request

def web_search(query: str) -> str:
    # Very naive search; replace with a real API in production

    url = f"https://duckduckgo.com/html/?q={urllib.parse.quote(query)}"
    html = urllib.request.urlopen(url).read().decode()
    # Return the first <a> href we can find

    start = html.find('href="') + 6
    end = html.find('"', start)
    return html[start:end]

if __name__ == "__main__":
    # Simple CLI for manual testing

    import sys
    print(json.dumps({"result": web_search(" ".join(sys.argv[1:]))}))

The accompanying schema in the lesson's docs/en.md specifies that this tool expects a single string argument named query, enabling the LLM to generate properly structured calls.

Testing Tool Execution

Validate the complete loop using the lesson's test suite:

cd phases/13-tools-and-protocols/02-function-calling-deep-dive/code
python3 -m unittest discover -v

The tests in code/tests/test_main.py verify that the Tool Registry correctly unmarshals model-generated JSON, invokes web_search, and propagates exceptions back to the LLM context.

Advanced Patterns and Production Considerations

The curriculum addresses production complexities in phases/11-llm-engineering/09-function-calling/outputs/skill-function-calling-patterns.md, including:

  • Parallel Tool Calls: Handling multiple simultaneous function invocations and aggregating results
  • Streaming Tool Results: Incrementally returning data for long-running operations
  • Guardrails: Security and cost-control mechanisms to prevent unauthorized tool execution or excessive API usage

A key architectural insight from Phase 13 is the Multi-Caller Protocol (MCP), which abstracts provider-specific function-calling APIs into a unified transport layer. This abstraction—analogous to HTTP for networking—allows the same tool definitions to execute across different LLM providers without modifying lesson code.

Summary

  • The ai-engineering-from-scratch repository teaches function calling through a rigorous four-step loop: definition, parsing, execution, and injection
  • Phase 13 (Tools & Protocols) and Phase 14 (Agent Engineering) contain the core curriculum for implementing function calling and tool use in AI agents
  • The Tool Registry pattern and MCP transport layer provide cross-provider portability for OpenAI, Anthropic, and Gemini APIs
  • The scripts/scaffold-lesson.sh automation ensures consistent, test-driven lesson development
  • Production-ready implementations require handling parallel calls, streaming results, and security guardrails as documented in the skill patterns output

Frequently Asked Questions

What is the MCP transport layer mentioned in the curriculum?

The Multi-Caller Protocol (MCP) is an architectural abstraction defined in Phase 13 that standardizes how tool definitions and execution requests flow between LLMs and host systems. According to the repository source code, MCP treats function calling as a transport layer similar to HTTP, enabling the same tool interfaces to work across OpenAI's function_call, Anthropic's tool_use, and Gemini's functionDeclarations without code changes.

How does the repository handle different LLM provider formats?

The curriculum normalizes provider differences through the Tool Registry pattern implemented in lesson code. While OpenAI returns function_call objects and Anthropic returns tool_use blocks, the registry parses these into a unified internal format before execution. This abstraction is detailed in phases/13-tools-and-protocols/02-function-calling-deep-dive/docs/en.md and reused in later capstone projects.

Where are the production security patterns documented?

Security guardrails and cost-control strategies for function calling are cataloged in phases/11-llm-engineering/09-function-calling/outputs/skill-function-calling-patterns.md. This file provides a decision framework for input validation, rate limiting, and error handling that prevents unauthorized tool execution and manages API expenditure in production environments.

Can I add custom tools to the existing curriculum?

Yes. The repository provides scripts/scaffold-lesson.sh to generate new lessons with the required structure (docs/, code/, code/tests/). When implementing custom tools, you must define the JSON schema in the lesson's documentation and implement the handler in code/main.py, following the four-step loop validated by the test suite in code/tests/.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →