Skip to main content
This example shows how to create agents that can call real Python functions. We’ll demonstrate both the low-level approach and the preferred Agent class approach.
1

Define the tool

2

Using the Agent class (Recommended)

The Agent class provides a more convenient interface with persistent state and better organization:
The Agent class is the recommended approach for most use cases. It provides persistent state, better error handling, and a cleaner interface for multi-turn conversations.
3

Low-level approach (Advanced)

For advanced use cases, you can use the lower-level generate_text function directly:
The SDK automatically loops until the model stops requesting tools (max 8 iterations by default). Each tool result is appended as a tool message so the model can reference previous calls.