Client Management
Client management helpers for persistent HTTP clients.
ClientManager()
Explicit, caller-owned tracker for clients created within one asyncio run.
Examples:
async with ClientManager() as manager:
client_config = ClientConfig(http_client=http_client, manager=manager)
invoker = AnthropicLMInvoker(..., client_config=client_config)
...
get_or_create() also works without the async with block -- construct a ClientManager()
directly and call release_resources() explicitly when done:
manager = ClientManager()
client_config = ClientConfig(http_client=http_client, manager=manager)
invoker = AnthropicLMInvoker(..., client_config=client_config)
...
await manager.release_resources()
Every client returned by get_or_create() is closed by release_resources() regardless of
whether the caller keeps its ClientConfig alive -- the manager tracks each client
independent of that config's lifetime.
Initializes a new, empty ClientManager.
__aenter__()
async
Enters the async context manager.
Returns:
| Name | Type | Description |
|---|---|---|
ClientManager |
ClientManager
|
This manager instance. |
__aexit__(*exc_info)
async
Closes every tracked client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exc_info
|
object
|
Exception info passed by the |
()
|
get_or_create(config, factory, identity, model_id_log)
Return the tracked client for config's identity if one exists, else build and track one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ClientConfig
|
The caller's client configuration. |
required |
factory
|
Callable[[], ManagedClient]
|
Builds a fresh client from
|
required |
identity
|
dict[str, Any]
|
The calling invoker's own connection parameters (api_key, base_url, organization, headers, timeout, ...). |
required |
model_id_log
|
str
|
Log identifier for the calling invoker. |
required |
Returns:
| Type | Description |
|---|---|
ManagedClient
|
tuple[ManagedClient, bool]: The live client and whether it is manager-tracked |
bool
|
( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
release_resources()
async
Closes every tracked client and marks this manager closed.
This includes clients built around a caller-supplied config.http_client: opting a
ClientConfig into a manager transfers close-ownership of its client to that manager,
even for an injected transport.
Concurrent calls are serialized so each tracked client is closed exactly once.
ManagedClient
Bases: Protocol
Structural shape a client tracker needs: something it can close and check.
close()
async
Closes the client and releases its connections.
is_closed()
Returns whether the client is closed.
PersistentClientMixin
Mixin for invokers that own a closable provider client, ephemeral or persistent.
Provides double-checked locking for safe client reacquisition after release_resources() in
persistent mode. Subclasses must call _register_client() in __init__ and implement _create_client().
release_resources()
async
Close the underlying persistent client, if any, and release its connections.
A safe no-op in ephemeral mode, since no ambient client is kept. In persistent mode, only
closes a client this invoker actually owns: manager-tracked clients are left for the
ClientManager to close, and caller-supplied client_config.http_client transports are
left for the caller to close.