AI Agents for Beginners - 4. Tool Use Design Pattern

How AI agents use tools via function calling — building blocks, tool schemas, approval modes, and practical examples with Microsoft Agent Framework and Azure AI Agent Service.
June 8, 2026

AI Agents for Beginners - 4. Tool Use Design Pattern

This article summarizes Lesson 04 of Microsoft's AI Agents for Beginners course.

What is the Tool Use Design Pattern?

The Tool Use Design Pattern allows an LLM to interact with external tools to achieve specific goals.
Tools are executable code for an agent, ranging from simple functions (calculators) to third-party API calls (stock quotes, weather forecasts).
Without tools, LLMs can only generate text. Adding tools empowers agents to act in the real world by responding to model-generated function calls.

Use Cases

The Tool Use Design Pattern applies to various scenarios requiring dynamic interaction with external systems:
Use CaseDescriptionExamples
Dynamic Information RetrievalRetrieve up-to-date data from external APIs or databasesSQLite data analysis, stock prices, weather info
Code ExecutionExecute code or scripts to solve math problems, generate reportsPython code execution, simulations
Workflow AutomationAutomate repetitive, multi-step workflowsEmail services, data pipelines
Customer SupportIntegrate with CRMs, ticketing platforms, knowledge basesAutomated resolution of user inquiries
Content GenerationIntegrate grammar checkers, text summarizers, etc.Support content creation and editing

Building Blocks

Core building blocks required to implement the Tool Use Design Pattern:
Building BlockDescription
Function/Tool SchemasDetailed schemas defining tool names, purposes, parameters, and outputs. Helps the LLM understand available tools and generate valid requests
Function Execution LogicLogic that determines whether and how to invoke tools based on user intent and conversation context
Message Handling SystemComponent managing conversation flow across user inputs, LLM responses, tool calls, and tool outputs
Tool Integration FrameworkInfrastructure connecting agents from simple functions to complex external services
Error Handling & ValidationHandles tool execution failures, parameter validation, and unexpected responses
State ManagementTracks conversation context, prior tool interactions, and persistent data to ensure multi-turn consistency

Function/Tool Calling

Function Calling is the core mechanism through which LLMs interact with tools.
Its implementation requires three components:

1. Initialize an LLM

Initialize an LLM that supports function calling. Azure OpenAI supports this:
init_llm.py
python

2. Create a Function Schema

Define the JSON schema to pass to the LLM. The LLM references this schema to select the appropriate function and generate arguments:
function_schema.py
python
The LLM returns which function to call with which arguments, rather than the final answer.

3. Execute the Function

Execute the selected function and pass the result back to the LLM to generate the final response:
function_execute.py
python
Function Calling Flow
Mermaid
zenuml title Function Calling Flow User->LLM: What time is it in San Francisco? LLM->LLM: select get_current_time from schema LLM->App: tool_call: get_current_time(location=San Francisco) App->Function: get_current_time("San Francisco") Function->App: {"current_time": "09:24 AM"} App->LLM: tool result added to messages LLM->User: The current time in San Francisco is 09:24 AM

Tool Use with Microsoft Agent Framework

MAF significantly simplifies tool definitions with the @tool decorator.
Docstrings become tool descriptions, and type annotations serve as parameter schemas that are automatically passed to the LLM.

Defining Tools

maf_tools.py
python

Using Multiple Tools

When multiple tools are registered with an agent, the LLM autonomously selects and invokes them as needed:
maf_multi_tools.py
python

Structured Output with Tools

By specifying a Pydantic model in response_format, you can receive tool results as structured JSON:
maf_structured_tools.py
python

Tool Approval Patterns

The approval_mode parameter controls whether human approval is required for each tool call:
ModeBehaviorUse Case
"never_require"Automatic execution — no user confirmation requiredRead-only queries, functions without side effects
"always_require"Requires user approval on every callFunctions with side effects such as payments or reservations
For tools with side effects, always use "always_require" to maintain a Human-in-the-loop:
maf_approval.py
python
Tool Approval Flow
Mermaid
flowchart TD Agent["Agent\nDecides tool call"] Agent --> Check{approval_mode?} Check -->|"never_require"| AutoRun["Automatic execution\n(Query, calculation, etc.)"] Check -->|"always_require"| HumanApproval["Request user approval"] HumanApproval --> Approved{Approved?} Approved -->|Yes| Execute["Execute tool"] Approved -->|No| Cancel["Cancel call"] AutoRun --> Result["Result → LLM"] Execute --> Result

Tool Use with Azure AI Agent Service

Azure AI Agent Service automatically handles tools on the server side.
You can combine multiple tools using ToolSet and take advantage of pre-built tools such as FunctionTool and CodeInterpreterTool.
The provided tools fall into two categories:
CategoryTools
Knowledge ToolsGrounding with Bing Search, File Search, Azure AI Search
Action ToolsFunction Calling, Code Interpreter, OpenAPI tools, Azure Functions
azure_ai_toolset.py
python
The LLM examines the toolset and autonomously selects the appropriate option between the custom function (fetch_sales_data_using_sqlite_query) and CodeInterpreterTool based on the user's request.

Security Considerations

When an LLM dynamically generates SQL, security risks such as SQL injection arise.
These risks can be effectively mitigated using the following principles:
  • Read-only Access — Grant only SELECT permissions to the DB (block INSERT/UPDATE/DELETE)
  • Isolated Environment — Use a read-only data warehouse separated from production systems
  • Parameter Validation — Validate and sanitize inputs before executing tools

Summary

Lesson 04 Summary
Mermaid
flowchart LR Root["Tool Use\nDesign Pattern"] Root --> How["How It Works"] Root --> MAF["Microsoft Agent\nFramework"] Root --> AAAS["Azure AI\nAgent Service"] Root --> Safety["Safety"] How --> H1["Function Schema"] How --> H2["LLM selects tool"] How --> H3["Execute & return"] MAF --> M1["@tool decorator"] MAF --> M2["Multiple tools"] MAF --> M3["approval_mode"] AAAS --> A1["ToolSet"] AAAS --> A2["Knowledge Tools"] AAAS --> A3["Action Tools"] Safety --> S1["Read-only DB"] Safety --> S2["Human-in-the-loop"]
  • The Tool Use Design Pattern empowers LLMs with the ability to take real-world actions through external tools
  • The core of Function Calling is the Define Schema → LLM selects function → Execute → Return results to LLM cycle
  • Easily convert Python functions into tools using the @tool decorator, and maintain human oversight with approval_mode
  • For tools with side effects, always use "always_require" to guarantee a Human-in-the-loop
Jooojub
System S/W engineer
Explore Tags
Series
    Recent Post
    © 2026. jooojub. All right reserved.