anyray-agent
Keeps an agent answering when its Anyray gateway is unreachable. Give your SDK an httpx2
client built on AnyrayTransport(). While the gateway is up, every call goes through it unchanged. When the gateway
is down, a call goes straight to the provider instead.
pip install anyray-agent
Python 3.11 or later. One dependency, httpx2, which the current OpenAI and Anthropic SDKs already use.
Use it
Keep the client pointed at your gateway, with your agent key, exactly as before. Add one argument.
import os
import httpx2
from anthropic import Anthropic
from anyray_agent import AnyrayTransport
client = Anthropic(
base_url=os.environ["ANYRAY_GATEWAY_URL"],
api_key=os.environ["ANYRAY_CLIENT_KEY"],
http_client=httpx2.Client(transport=AnyrayTransport()),
)
| Client | Where AnyrayTransport() goes |
|---|---|
| OpenAI SDK | OpenAI(..., http_client=httpx2.Client(transport=AnyrayTransport())) |
| Anthropic SDK | Anthropic(..., http_client=httpx2.Client(transport=AnyrayTransport())) |
LangChain ChatOpenAI |
ChatOpenAI(..., http_client=httpx2.Client(transport=AnyrayTransport())) |
LangChain ChatAnthropic |
use_fallback(ChatAnthropic(...), transport=AnyrayTransport()) |
use_fallback comes from anyray_agent.langchain: ChatAnthropic has no client argument, so it swaps the clients
the model builds. Async code uses AsyncAnyrayTransport with httpx2.AsyncClient (http_async_client= on
ChatOpenAI, async_transport= on use_fallback). An SDK that still requires httpx 0.x instead of httpx2
(pip show openai or pip show anthropic lists which) takes the same classes from anyray_agent.httpx, in an
httpx client (pip install "anyray-agent[httpx]"). use_fallback builds httpx2 clients, so upgrade an
anthropic still on httpx 0.x before you use it.
The provider key
A call can only go direct with a provider key on the host: OPENAI_API_KEY and ANTHROPIC_API_KEY are read from the
environment, or pass them:
AnyrayTransport(provider_keys={"openai": "…", "anthropic": "…"})
To send Anthropic calls to AWS Bedrock instead of api.anthropic.com, name the region. Credentials come from
AWS_BEARER_TOKEN_BEDROCK or the standard AWS variables, or pass api_key or credentials. For an IAM role or a
profile, pass boto3's credential chain:
import boto3
AnyrayTransport(bedrock={"region": "us-east-1", "credentials": boto3.Session().get_credentials})
Without a key for a provider, its calls are never sent direct.
When a call goes direct
- Sent direct: the connection to the gateway never opened (refused, DNS, connect timeout) and the gateway's
/livezcheck also fails. After the outage has lasted your org's fallback time (120 seconds by default), calls go direct without trying the gateway first. They return once the gateway has answered for your org's recover time (10 minutes by default). While the gateway seems down, the agent checks/livezat your org's check interval, and a check slower than your org's check timeout counts as failed (every 60 seconds and 5 seconds by default). - Never sent direct: a call that may already have reached the provider (a reset mid-request, any 5xx), so nothing is billed twice. Also a model or route only the gateway serves, and any call while your org has turned fallback off. Nothing goes direct until the agent has read that setting from the gateway.
The gateway key is never sent to a provider.
See what it is doing
transport = AnyrayTransport(on_status=lambda status: print(status.state))
transport.status() # FallbackStatus(state="armed" | "direct" | "blocked" | "off", reason=..., since=..., calls_direct=...)
The agent also reports its state to the gateway, without any request content, so it shows on the Agents page.
Full reference: docs.anyray.ai/enroll/agent-fallback-reference.
MIT licensed.
Metadata
Release files for anyray-agent 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| anyray_agent-0.1.4.tar.gz | 17.7 kB | Without provenance | Details for anyray_agent-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| anyray_agent-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for anyray_agent-0.1.4-py3-none-any.whl |
Total release size: 40.7 kB
Release files / anyray_agent-0.1.4.tar.gz
| Download URL | anyray_agent-0.1.4.tar.gz |
|---|---|
| Size | 17.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00de9a08a991d51401e2c88fcd90a1a14533efd0dfb28c670228612933a69cf2
|
|
BLAKE2b-256 checksum How to use checksums |
47ff638faaa6489c018fb82b7b2173da51194aab0c56f87ee151c60bcca59703
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.11.17
|
Release files / anyray_agent-0.1.4-py3-none-any.whl
| Download URL | anyray_agent-0.1.4-py3-none-any.whl |
|---|---|
| Size | 23.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
612066d943d4302faf8705dd8f5d1a5ea0e9d886dfa38a0f20f3dc9d395febbc
|
|
BLAKE2b-256 checksum How to use checksums |
f1ac43e4c6fc60b7495101d1c12a8eda24949a6d9c062b859036e67004d44a80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.11.17
|
Security for anyray-agent 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.