Defining Tools
Tools are Python methods marked with @function_tool. The SDK automatically generates the schema the LLM needs based on your function’s signature and docstring.
Basic Tool
The @function_tool() decorator marks a method as callable by the LLM.
The LLM sees this as a callable function with a description and typed parameters.
How It Works
@function_tool()marks the method as a tool- Type hints tell the LLM what types to pass
- Docstring explains when and how to use the tool
- Return value is sent back to the LLM
Docstring Format
The LLM reads your docstring to understand when to call the tool.
Parameter Types
Type hints define the schema the LLM uses to pass arguments.
Enum Parameters
Constrain values with Literal from typing.
Async Tools
Prefix with async def for database calls, HTTP requests, or other I/O.
Accessing Session State
Tools can read self. attributes for user data, DB connections, etc.
Error Handling
Return {"error": "message"} so the LLM can respond gracefully.
The LLM will say something like “I couldn’t find that order” instead of the conversation breaking.
Tips
Keep descriptions concise
The LLM reads tool descriptions every turn. Shorter = fewer tokens = faster.
Use verb + noun naming
Good: get_order_status, create_appointment, search_products. Bad: order, handle_booking.
Guide tool usage in the prompt
Tell the LLM when to use tools: “Use get_order_status when the user asks about an order.”

