@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.
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.| Part | Purpose |
|---|---|
| First line | Brief description (shown to LLM) |
| Second paragraph | When to use this tool |
| Args | Parameter descriptions |
Parameter Types
Type hints define the schema the LLM uses to pass arguments.Enum Parameters
Constrain values withLiteral from typing.
Async Tools
Prefix withasync def for database calls, HTTP requests, or other I/O.
Accessing Session State
Tools can readself. attributes for user data, DB connections, etc.
Error Handling
Return{"error": "message"} so the LLM can respond gracefully.
Tips
Keep descriptions concise
Keep descriptions concise
The LLM reads tool descriptions every turn. Shorter = fewer tokens = faster.
Use verb + noun naming
Use verb + noun naming
Good:
get_order_status, create_appointment, search_products. Bad: order, handle_booking.Guide tool usage in the prompt
Guide tool usage in the prompt
Tell the LLM when to use tools: “Use get_order_status when the user asks about an order.”

