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

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.2
File Size Uploaded Provenance Actions
anyray_agent-0.1.2.tar.gz 17.1 kB Without provenance Details for anyray_agent-0.1.2.tar.gz

Built distribution (wheel)

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

Total release size: 39.4 kB

Release files / anyray_agent-0.1.2.tar.gz

Download URL anyray_agent-0.1.2.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2e3799e529a6e32e48ae3a1e2265b59583fb482643ee438288fd17ba6d05d01b
BLAKE2b-256 checksum
How to use checksums
33996b12340920db9e3eaf87c1b7623b20d2734770068d7a71e14da3813174cc
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.2-py3-none-any.whl

Download URL anyray_agent-0.1.2-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a84545d6345c8e642c57eb89ba691de7ee547f9b133b0f1ff04944b94acb8f2
BLAKE2b-256 checksum
How to use checksums
07d49d710e58cef86f13fc03ea98c39a8350516143a959d9cedc6a7bd02ab421
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.2

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