Client Lifecycle

The SDK manages HTTP resources for you, but you still need to close the client when you are done.

from touai import TouAI
 
with TouAI() as client:
    results = client.web_access.search("quarterly revenue", limit=5)
    print(results.total_results)

When used as a context manager, the underlying HTTP connections are released automatically.

Manual Lifecycle

from touai import TouAI
 
client = TouAI()
try:
    results = client.web_access.search("quarterly revenue", limit=5)
    print(results.total_results)
finally:
    client.close()

Async Client

AsyncTouAI exposes the same nine service resources as TouAI, with every method awaitable — each call runs the sync implementation in a worker thread, so the event loop is never blocked by network I/O.

from touai import AsyncTouAI
 
async with AsyncTouAI() as client:
    files = await client.object_storage.files()

Outside a context manager, call await client.close().

Helpers that return a lazy iterator or SSE stream (paginated iter_* helpers, streaming) hand back the sync iterator even on AsyncTouAI — each page or event fetch then runs synchronously in the consuming task. For heavy streaming consumption, prefer the sync client in a dedicated thread.

When to Choose Each Style

PatternBest For
Context managerscripts, CLIs, short-lived jobs
Manual close()long-lived services with explicit lifecycle management
AsyncTouAIasyncio applications (web servers, agents)

If you create a client inside helper functions or background jobs, prefer the context manager form to avoid leaking network resources.

ConfigurationContext Layer