pyagent-providers API Reference¶
pyagent_providers
¶
PyAgent Providers — multi-provider abstraction with capability negotiation and fallback chains.
HealthStatus
¶
ProviderCapabilities
dataclass
¶
Declares what a provider can do.
Attributes:
| Name | Type | Description |
|---|---|---|
models |
list[str]
|
List of model identifiers this provider exposes. |
capabilities |
set[Capability]
|
Set of capability tags (reuses pyagent_router.Capability). |
max_context |
int
|
Maximum context window in tokens across all models. |
supports_streaming |
bool
|
Whether the provider supports token-level streaming. |
supports_tools |
bool
|
Whether the provider supports tool/function calling. |
supports_vision |
bool
|
Whether the provider supports image inputs. |
Source code in packages/pyagent-providers/src/pyagent_providers/base.py
ProviderInfo
dataclass
¶
Snapshot of provider state returned by the registry.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Provider name. |
capabilities |
ProviderCapabilities
|
Provider capabilities. |
health |
HealthStatus
|
Current health status. |
metadata |
dict[str, Any]
|
Arbitrary extra metadata (e.g. region, version). |
Source code in packages/pyagent-providers/src/pyagent_providers/base.py
ProviderProtocol
¶
Bases: Protocol
Interface that every LLM provider must implement.
Also satisfies LLMCallable via __call__ which delegates to
complete with a default model.
Source code in packages/pyagent-providers/src/pyagent_providers/base.py
name
property
¶
Unique identifier for this provider.
capabilities
property
¶
Declared capabilities.
health()
async
¶
complete(messages, model=None)
async
¶
Generate a completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation history. |
required |
model
|
str | None
|
Optional model override. Uses provider default if |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The assistant response text. |
Source code in packages/pyagent-providers/src/pyagent_providers/base.py
CostOptimizer
¶
Compare costs across all registered providers.
Wraps CostEstimator from pyagent-router and iterates over every
provider's model list to find the cheapest option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ProviderRegistry
|
Provider registry to draw models from. |
required |
cost_estimator
|
CostEstimator | None
|
Optional |
None
|
Source code in packages/pyagent-providers/src/pyagent_providers/cost.py
compare(task, *, healthy_only=True, limit=10)
¶
Estimate cost for every provider + model pair and rank cheapest first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
str
|
The task text to estimate. |
required |
healthy_only
|
bool
|
If |
True
|
limit
|
int
|
Maximum number of results to return. |
10
|
Returns:
| Type | Description |
|---|---|
list[ProviderCostEstimate]
|
Sorted list of |
Source code in packages/pyagent-providers/src/pyagent_providers/cost.py
cheapest(task, *, healthy_only=True)
¶
Return the single cheapest provider + model for a task.
Returns:
| Type | Description |
|---|---|
ProviderCostEstimate | None
|
The cheapest option, or |
Source code in packages/pyagent-providers/src/pyagent_providers/cost.py
cheapest_provider(task, *, healthy_only=True)
¶
Return the cheapest provider object + model name.
Convenience method for passing directly to Agent or patterns.
Source code in packages/pyagent-providers/src/pyagent_providers/cost.py
ProviderCostEstimate
dataclass
¶
Cost estimate for a specific provider + model pair.
Attributes:
| Name | Type | Description |
|---|---|---|
provider_name |
str
|
Provider that would serve this request. |
model |
str
|
Model within that provider. |
estimate |
CostEstimate
|
The underlying cost estimate. |
Source code in packages/pyagent-providers/src/pyagent_providers/cost.py
FallbackChain
¶
Try providers in order until one succeeds.
Optionally integrates with CircuitBreaker from pyagent-patterns
to skip providers whose circuits are open.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
providers
|
list[ProviderProtocol]
|
Ordered list of providers to try. |
required |
circuit_breakers
|
dict[str, Any] | None
|
Optional mapping of provider name → CircuitBreaker. If a provider's circuit is open, it is skipped. |
None
|
Source code in packages/pyagent-providers/src/pyagent_providers/fallback.py
complete(messages, model=None)
async
¶
Try each provider in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation messages. |
required |
model
|
str | None
|
Optional model override passed to each provider. |
None
|
Returns:
| Type | Description |
|---|---|
FallbackResult
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If all providers fail. |
Source code in packages/pyagent-providers/src/pyagent_providers/fallback.py
__call__(messages)
async
¶
FallbackResult
dataclass
¶
Result of a fallback chain execution.
Attributes:
| Name | Type | Description |
|---|---|---|
output |
str
|
The completion text from the first successful provider. |
provider_name |
str
|
Name of the provider that succeeded. |
attempts |
list[FallbackAttempt]
|
Ordered log of every attempt. |
Source code in packages/pyagent-providers/src/pyagent_providers/fallback.py
CapabilityNegotiator
¶
Find the best provider for a given set of task requirements.
Scores providers by capability overlap, context window adequacy, and feature support (streaming, tools, vision).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ProviderRegistry
|
Provider registry to search. |
required |
Source code in packages/pyagent-providers/src/pyagent_providers/negotiation.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
negotiate(required_capabilities=None, *, min_context=0, needs_streaming=False, needs_tools=False, needs_vision=False)
¶
Find the best provider matching the requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_capabilities
|
set[Capability] | None
|
Must-have capabilities. |
None
|
min_context
|
int
|
Minimum context window size in tokens. |
0
|
needs_streaming
|
bool
|
Whether streaming is required. |
False
|
needs_tools
|
bool
|
Whether tool/function calling is required. |
False
|
needs_vision
|
bool
|
Whether image input is required. |
False
|
Returns:
| Type | Description |
|---|---|
NegotiationResult | None
|
Best |
Source code in packages/pyagent-providers/src/pyagent_providers/negotiation.py
negotiate_all(required_capabilities=None, *, min_context=0, needs_streaming=False, needs_tools=False, needs_vision=False, limit=5)
¶
Return all matching providers ranked by score.
Same args as negotiate plus limit to cap results.
Source code in packages/pyagent-providers/src/pyagent_providers/negotiation.py
NegotiationResult
dataclass
¶
Result of capability negotiation.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
ProviderProtocol
|
The best-matched provider. |
model |
str
|
Recommended model from that provider. |
match_score |
float
|
How well the provider matches (0.0-1.0). |
matched_capabilities |
set[Capability]
|
Which requested capabilities the provider satisfies. |
missing_capabilities |
set[Capability]
|
Which requested capabilities the provider lacks. |
Source code in packages/pyagent-providers/src/pyagent_providers/negotiation.py
ProviderRegistry
¶
Central registry of all available LLM providers.
Supports registration, discovery by capability, and periodic health checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auto_health_check
|
bool
|
If |
False
|
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
register(provider, metadata=None)
async
¶
Register a provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
ProviderProtocol
|
Any object satisfying |
required |
metadata
|
dict[str, Any] | None
|
Optional extra metadata (region, version, etc.). |
None
|
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
register_sync(provider, metadata=None)
¶
Synchronous registration (skips health check).
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
remove(name)
¶
get(name)
¶
list_providers()
¶
Return info snapshots for all registered providers.
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
discover(required_capabilities=None, *, healthy_only=True)
¶
Find providers matching capability and health requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_capabilities
|
set[Capability] | None
|
If given, only return providers whose capabilities are a superset of this set. |
None
|
healthy_only
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
list[ProviderProtocol]
|
List of matching providers. |
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
check_health(name=None)
async
¶
Run health checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
If given, check only this provider. Otherwise check all. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, HealthStatus]
|
Mapping of provider name → health status. |
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
remove_unhealthy()
async
¶
Run health checks and remove all unhealthy providers.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of removed provider names. |
Source code in packages/pyagent-providers/src/pyagent_providers/registry.py
ProviderRouter
¶
Route requests to the best provider + model pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
ProviderRegistry
|
The provider registry to draw from. |
required |
strategy
|
RoutingStrategy
|
Routing strategy to use. |
CAPABILITY_FIRST
|
cost_estimator
|
CostEstimator | None
|
Optional cost estimator for |
None
|
scorer
|
DifficultyScorer | None
|
Optional difficulty scorer for capability-based strategies. |
None
|
Source code in packages/pyagent-providers/src/pyagent_providers/router.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
route(messages, required=None)
async
¶
Select the best provider and model for a request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[Message]
|
Conversation messages (last user message used for scoring). |
required |
required
|
set[Capability] | None
|
Optional capability requirements. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ProviderProtocol, str]
|
Tuple of (provider, model_name). |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no suitable provider is found. |
Source code in packages/pyagent-providers/src/pyagent_providers/router.py
RoutingStrategy
¶
Bases: StrEnum
How the router picks a provider + model pair.
Source code in packages/pyagent-providers/src/pyagent_providers/router.py
TracedProvider
¶
Wrapper that emits trace events for every complete() / __call__ invocation.
Wraps any ProviderProtocol implementation without modifying the original.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
ProviderProtocol
|
The underlying provider to delegate calls to. |
required |
trace_bus
|
Any
|
A |
required |
Source code in packages/pyagent-providers/src/pyagent_providers/traced.py
complete(messages, model=None)
async
¶
Delegate to wrapped provider and emit trace events.
Source code in packages/pyagent-providers/src/pyagent_providers/traced.py
__call__(messages)
async
¶
LLMCallable-compatible entry point — delegates to complete.
TracedProvider¶
pyagent_providers.traced.TracedProvider
¶
Wrapper that emits trace events for every complete() / __call__ invocation.
Wraps any ProviderProtocol implementation without modifying the original.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
ProviderProtocol
|
The underlying provider to delegate calls to. |
required |
trace_bus
|
Any
|
A |
required |
Source code in packages/pyagent-providers/src/pyagent_providers/traced.py
__call__(messages)
async
¶
LLMCallable-compatible entry point — delegates to complete.
complete(messages, model=None)
async
¶
Delegate to wrapped provider and emit trace events.