Tools extend agent capabilities by enabling them to retrieve knowledge, execute code, call external APIs, and interact with enterprise systems. This guide explains tool concepts and how to use them effectively.
Capability: Execute Python code in a sandboxed environmentUse cases:
Mathematical calculations and data analysis
Generate charts and visualizations
Process CSV and data files
Perform iterative problem-solving
Example:
from azure.ai.projects.models import CodeInterpreterToolcode_interpreter = CodeInterpreterTool()agent = project.agents.create_agent( model="gpt-4o", name="data-analyst", instructions="You can analyze data and create visualizations.", tools=code_interpreter.definitions, tool_resources=code_interpreter.resources,)
Features:
Automatic retry on code failures
Support for popular Python libraries
File upload and download
Session persistence (1 hour default, 30 min idle timeout)
Code Interpreter has additional charges beyond token-based fees. Each concurrent session is billed separately.
Capability: Define custom functions for agents to callUse cases:
Call internal APIs
Query databases
Trigger workflows
Integrate business logic
Example:
from azure.ai.projects.models import FunctionToolimport jsondef get_order_status(order_id: str) -> str: """ Get the status of a customer order. :param order_id: The order ID to look up :return: Order status as JSON string """ # Call your API or database status = fetch_order_from_db(order_id) return json.dumps({"order_id": order_id, "status": status})# Register functionfunction_tool = FunctionTool(functions={get_order_status})agent = project.agents.create_agent( model="gpt-4o", name="order-assistant", instructions="Help customers check their order status.", tools=function_tool.definitions,)# Handle function callsrun = project.agents.runs.create(thread_id=thread.id, agent_id=agent.id)while run.status in ["queued", "in_progress", "requires_action"]: if run.status == "requires_action": tool_outputs = [] for tool_call in run.required_action.submit_tool_outputs.tool_calls: if tool_call.function.name == "get_order_status": args = json.loads(tool_call.function.arguments) output = get_order_status(args["order_id"]) tool_outputs.append({ "tool_call_id": tool_call.id, "output": output }) # Submit results project.agents.runs.submit_tool_outputs( thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs ) run = project.agents.runs.get(thread_id=thread.id, run_id=run.id)
User: "What's the weather in my favorite city?"1. Call getUserFavoriteCity() -> "Seattle"2. Call getWeather("Seattle") -> "Sunny, 72°F"3. Generate response: "It's sunny and 72°F in Seattle"
User: "I need help with my order"1. Analyze request type2. If status inquiry -> call getOrderStatus()3. If cancellation -> call cancelOrder()4. If modification -> call modifyOrder()
The model uses your tool descriptions to decide when to call them:
def get_weather(location: str, unit: str = "fahrenheit") -> str: """ Get current weather for a specific location. Use this tool when the user asks about weather conditions, temperature, or forecast for a particular city or location. :param location: City and state/country (e.g., "Seattle, WA") :param unit: Temperature unit - "fahrenheit" or "celsius" :return: Current weather conditions as JSON """