Azure-AI_Foundry

Lesson 2: Agent Tools

Learning Objectives

Reference: Agent Tools Overview


Tools Overview

Tools extend an agent’s capabilities beyond text generation. The agent decides when and how to use tools based on the user’s request and the agent’s instructions.

Tool Purpose Key Use Case
Code Interpreter Execute Python in a sandbox Data analysis, chart generation, math
File Search Vector search over uploaded files Q&A over documents, knowledge bases
Bing Search Live web search Current events, real-time grounding
Azure AI Search Enterprise index search Corporate data, structured retrieval
Function Calling Call your custom functions Database queries, API calls, business logic
Azure Functions Serverless tool execution Scalable, event-driven tool backends
OpenAPI Call external REST APIs Third-party integrations

1. Code Interpreter

Code Interpreter lets the agent write and execute Python code in a secure sandbox. It can generate files (charts, CSVs), perform calculations, and analyze uploaded data.

from azure.ai.projects.models import CodeInterpreterTool

agent = client.agents.create_agent(
    model="gpt-4o",
    name="data-analyst",
    instructions="Analyze data files. Create visualizations when helpful.",
    tools=CodeInterpreterTool().definitions,
)

# Upload a file for the agent to analyze
file = client.agents.upload_file(file_path="sales_data.csv", purpose="agents")

# Attach the file to a message
client.agents.create_message(
    thread_id=thread.id,
    role="user",
    content="Summarize the sales trends and create a bar chart.",
    attachments=[{"file_id": file.id, "tools": [{"type": "code_interpreter"}]}],
)

Tip: Code Interpreter runs in an isolated sandbox – it cannot access the internet or your local files. Upload everything the agent needs.


File Search enables vector-based retrieval over uploaded documents. The service automatically chunks, embeds, and indexes your files.

from azure.ai.projects.models import FileSearchTool

vector_store = client.agents.create_vector_store(name="policy-docs")
client.agents.create_vector_store_file_batch(
    vector_store_id=vector_store.id, file_ids=[file1.id, file2.id]
)

agent = client.agents.create_agent(
    model="gpt-4o",
    name="policy-assistant",
    instructions="Answer questions using the uploaded policy documents.",
    tools=FileSearchTool().definitions,
    tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

3. Bing Search (Web Grounding)

Bing Search grounds agent responses in live web data. Requires a Bing Grounding connection in your AI Foundry project.

from azure.ai.projects.models import BingGroundingTool

bing_connection = client.connections.get(connection_name="bing-grounding")
agent = client.agents.create_agent(
    model="gpt-4o",
    name="news-assistant",
    instructions="Answer questions with current information from the web.",
    tools=BingGroundingTool(connection_id=bing_connection.id).definitions,
)

Azure AI Search connects agents to your enterprise search indexes for grounded, factual responses over corporate data.

from azure.ai.projects.models import AzureAISearchTool

search_connection = client.connections.get(connection_name="my-search")
search_tool = AzureAISearchTool(
    index_connection_id=search_connection.id, index_name="products-index"
)

agent = client.agents.create_agent(
    model="gpt-4o",
    name="product-advisor",
    instructions="Help customers find products using the product catalog.",
    tools=search_tool.definitions,
)

5. Function Calling (Custom Tools)

Function calling lets you define custom tools the agent can invoke. You provide the schema; the agent decides when to call them; you execute the logic and return results.

from azure.ai.projects.models import FunctionTool, ToolSet
import json

functions = FunctionTool(functions=[
    {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "The city name"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["city"],
        },
    },
])

agent = client.agents.create_agent(
    model="gpt-4o",
    name="weather-assistant",
    instructions="Help users check weather. Always use celsius unless asked.",
    tools=functions.definitions,
)

# After creating a run, check for requires_action status
if run.status == "requires_action":
    tool_calls = run.required_action.submit_tool_outputs.tool_calls
    tool_outputs = []
    for call in tool_calls:
        if call.function.name == "get_weather":
            args = json.loads(call.function.arguments)
            result = {"temperature": 22, "condition": "Sunny"}
            tool_outputs.append({
                "tool_call_id": call.id,
                "output": json.dumps(result),
            })
    client.agents.submit_tool_outputs_to_run(
        thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs
    )

6. Azure Functions

Azure Functions provide serverless backends for agent tools, offering automatic scaling and managed infrastructure.

from azure.ai.projects.models import AzureFunctionTool

azure_function_tool = AzureFunctionTool(
    name="process_order",
    description="Process a customer order and return confirmation.",
    parameters={
        "type": "object",
        "properties": {
            "order_id": {"type": "string"},
            "action": {"type": "string", "enum": ["confirm", "cancel"]},
        },
        "required": ["order_id", "action"],
    },
    input_binding={"type": "storage_queue", "queue_name": "order-requests"},
    output_binding={"type": "storage_queue", "queue_name": "order-responses"},
)

7. OpenAPI Tools

OpenAPI tools let agents call external REST APIs defined by an OpenAPI specification.

from azure.ai.projects.models import OpenApiTool

openapi_spec = open("petstore.json").read()
openapi_tool = OpenApiTool(
    name="petstore",
    description="Manage pet store inventory.",
    spec=openapi_spec,
    auth={"type": "anonymous"},
)

agent = client.agents.create_agent(
    model="gpt-4o", name="petstore-agent", tools=openapi_tool.definitions
)

Combining Multiple Tools

Agents can use multiple tools simultaneously:

toolset = ToolSet()
toolset.add(CodeInterpreterTool())
toolset.add(FileSearchTool())
toolset.add(functions)

agent = client.agents.create_agent(
    model="gpt-4o",
    name="multi-tool-agent",
    instructions="Use code interpreter for analysis, file search for docs, custom functions for live data.",
    tools=toolset.definitions,
    tool_resources=toolset.resources,
)

Tip: Start with fewer tools and add more as needed. Too many tools can confuse the agent and slow down tool selection.


Key Takeaways