Skip to main content

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 /livez check 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).
  • 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.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anyray-agent 0.1.3
File Size Uploaded Provenance Actions
anyray_agent-0.1.3.tar.gz 17.2 kB Without provenance Details for anyray_agent-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anyray-agent 0.1.3
File Interpreter ABI Platform Actions
anyray_agent-0.1.3-py3-none-any.whl Python 3 none any Details for anyray_agent-0.1.3-py3-none-any.whl

Total release size: 39.5 kB

Release files / anyray_agent-0.1.3.tar.gz

Download URL anyray_agent-0.1.3.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
65aa64a03f1af2499cc9de4d25ef01cc6ad8bd10b0a5c6456af33dbd79b052d2
BLAKE2b-256 checksum
How to use checksums
0f622fd4dffc340e20bdf9570d0b800db2703a3a1fcea675ea1a3b7a1a118598
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.3-py3-none-any.whl

Download URL anyray_agent-0.1.3-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4d02aea687c46936ba611bddca74e20d1d15add6ed05378378aed345c2d1e8e
BLAKE2b-256 checksum
How to use checksums
f1e8b17cc8a685e088754483cd1cc7dbc50f5b3a8383b49b73575ae9dc488f2b
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.3

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page