Defining Tools
Use the@tool decorator to create a tool from any function:
The @tool Decorator
The decorator accepts this optional parameter:
Parameter Definitions with Field
Use Field to describe each parameter:
Supported Parameter Types
ai-query supports these Python types:str- String valuesint- Integer valuesfloat- Floating point numbersbool- Boolean valueslist[T]- Arrays with inferred item typesdict[str, T]- Objects with typed valuestuple[T1, T2]andtuple[T, ...]- Fixed or variable-length arraysOptional[T],Union[...], andLiteral[...]- Optional, union, and enum-like valuesTypedDictand dataclasses - Nested object schemas
string. For full control, create a tool with an explicit parameters schema.
Using Tools with generate_text
Pass tools as a dictionary:Sync vs Async Tools
Tools can be either synchronous or asynchronous:Automatic Tool Execution
When you provide tools, ai-query automatically:- Sends the tool definitions to the AI
- Executes any tools the AI calls
- Returns the results to the AI
- Repeats until the AI responds without tool calls
Accessing Tool Calls and Results
Inspect what tools were called:Limiting Execution Steps
Control the maximum number of tool execution loops usingstop_when:
Next Steps
Agents
Build autonomous agents with advanced loop control