Skip to main content
Tools enable AI models to call functions in your code. ai-query provides a type-safe way to define tools using Python decorators and type hints.

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 values
  • int - Integer values
  • float - Floating point numbers
  • bool - Boolean values
  • list[T] - Arrays with inferred item types
  • dict[str, T] - Objects with typed values
  • tuple[T1, T2] and tuple[T, ...] - Fixed or variable-length arrays
  • Optional[T], Union[...], and Literal[...] - Optional, union, and enum-like values
  • TypedDict and dataclasses - Nested object schemas
If a type annotation is not recognized, inference falls back to 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:
  1. Sends the tool definitions to the AI
  2. Executes any tools the AI calls
  3. Returns the results to the AI
  4. 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 using stop_when:

Next Steps

Agents

Build autonomous agents with advanced loop control