Back to Pydantic Ai

Thread Executor

docs/capabilities/thread-executor.md

2.14.1870 B
Original Source

Thread Executor

The [ThreadExecutor][pydantic_ai.capabilities.ThreadExecutor] capability provides a custom [Executor][concurrent.futures.Executor] for running sync tool functions and other sync callbacks in threads. This is useful in long-running servers (e.g. FastAPI) where the default ephemeral threads from [anyio.to_thread.run_sync][anyio.to_thread.run_sync] can accumulate under sustained load:

python
from concurrent.futures import ThreadPoolExecutor

from pydantic_ai import Agent
from pydantic_ai.capabilities import ThreadExecutor

executor = ThreadPoolExecutor(max_workers=16, thread_name_prefix='agent-worker')
agent = Agent('openai:gpt-5.2', capabilities=[ThreadExecutor(executor)])

See Thread executor for long-running servers for more details.