Connections#

By default, each call to Enkrypt opens and closes its own HTTP connection. Nothing outlives the call or belongs to an event loop, so guards work the same under agent.run, agent.run_sync and repeated runs.

To reuse connections, pass your own httpx.AsyncClient. It is used as is, and closing it is yours:

import httpx

from pydantic_ai_enkrypt import Enkrypt

async with httpx.AsyncClient(timeout=30) as http:
    enkrypt = Enkrypt(http_client=http)
    agent = build_agent(enkrypt)
    result = await agent.run(prompt)

An httpx.AsyncClient belongs to the event loop it was created on. Create it inside the loop that runs the agent; agent.run_sync starts a new loop each time, so pass a client only with agent.run.

Enkrypt(timeout=30.0) sets the timeout of the connections it opens itself; a client you pass keeps its own.