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 still on httpx 0.x (openai < 3, anthropic < 2) takes the same classes from anyray_agent.httpx (pip install "anyray-agent[httpx]").

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.1

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.1
File Size Uploaded Provenance Actions
anyray_agent-0.1.1.tar.gz 17.0 kB Without provenance Details for anyray_agent-0.1.1.tar.gz

Built distribution (wheel)

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

Total release size: 39.2 kB

Release files / anyray_agent-0.1.1.tar.gz

Download URL anyray_agent-0.1.1.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e43ea03124164f19f7b361490600616f185aac329bc11b0563952081f00e5049
BLAKE2b-256 checksum
How to use checksums
f7205affeb2326edfb55876eb5fe2a85a73dbde9290e9d715669a152dcbddbe8
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.1-py3-none-any.whl

Download URL anyray_agent-0.1.1-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8a20fe3b506afc1f4d8d999491027d074432065d09e35d3a6691cfbd7d4bb5f3
BLAKE2b-256 checksum
How to use checksums
dcc5b10a3aa4e0a878cb0840c2459225daa6cb51a09e29ae66c2e3450d5b18ae
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.1

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