Skip to main content

anycast-agent

Python SDK for Anycast Agents P2P connectivity.

Connect agents to the Anycast network for peer-to-peer communication with relay fallback, powered by the global Anycast edge infrastructure.

Installation

pip install anycast-agent

Quick Start

import asyncio
from anycast_agent import AnycastAgent

async def main():
    agent = AnycastAgent(
        rendezvous_url="wss://agents.anycast.com/rendezvous",
        token="agt_your_token_here",
    )

    @agent.on_connect
    async def on_connect(info):
        print(f"Connected as {info.agent_id}")

    @agent.on_peer
    async def on_peer(agent_id, status):
        print(f"Peer {agent_id} is {status}")

    await agent.connect()
    await agent.run_forever()

asyncio.run(main())

API Reference

AnycastAgent(rendezvous_url, token, **kwargs)

Create a new agent instance.

Parameter Type Default Description
rendezvous_url str required WebSocket URL of the rendezvous server
token str required Agent auth token from the Anycast portal
machine_id str auto-generated Unique machine identifier
public_key str auto-generated Public key for E2E encryption
version str None Agent version string
reconnect bool True Auto-reconnect on disconnect
reconnect_interval float 3.0 Base reconnect interval (seconds)
timeout float 30.0 Connection timeout (seconds)
heartbeat_interval float 25.0 Heartbeat interval (seconds)

Properties

Property Type Description
agent_id str | None Assigned agent ID (set after connect)
tenant_id str | None Tenant ID (set after connect)
connected bool Whether the agent is connected
connection_state ConnectionState Current state

Methods

await agent.connect() -> AgentInfo

Connect to the rendezvous server. Returns AgentInfo with agent_id, tenant_id, name.

await agent.disconnect()

Disconnect from the server.

await agent.run_forever()

Block until disconnected. Handles reconnection automatically.

agent.request_connection(target_agent_id) -> str

Request a P2P connection to another agent. Returns a session ID.

await agent.send_signal(session_id, target_agent_id, signal)

Send a WebRTC signal (offer/answer/ICE candidate).

await agent.disconnect_session(session_id)

Close a specific session.

Events

Register handlers with decorators or .on():

# Decorator style
@agent.on_connect
async def handler(info): ...

# Method style
agent.on("connect", handler)
Event Handler Signature Description
connect (info: AgentInfo) Connected and registered
disconnect (reason: str) Disconnected
reconnecting (attempt: int) Attempting reconnection
peer (agent_id: str, status: str) Peer online/offline
connect_incoming (session_id, from_id, from_name) Incoming connection
connect_accepted (session_id, agent_id, public_key) Connection accepted
connect_rejected (session_id, reason) Connection rejected
signal (session_id, from_id, signal) WebRTC signal
relay_assigned (session_id, host, port, token) Relay assigned
error (error: AnycastError) Error occurred

Handlers can be sync or async — async handlers are awaited automatically.

Exceptions

from anycast_agent import (
    AnycastError,          # Base error (has .code, .fatal)
    AnycastConnectionError, # WebSocket connection failed
    AnycastAuthError,       # Token invalid (fatal)
    AnycastTimeoutError,    # Connection/registration timeout
)

ConnectionState

from anycast_agent import ConnectionState

ConnectionState.DISCONNECTED
ConnectionState.CONNECTING
ConnectionState.CONNECTED
ConnectionState.RECONNECTING

Auto-Reconnection

By default, the agent reconnects with exponential backoff (3s, 6s, 12s, ..., max 30s). Disable with reconnect=False.

License

MIT

Metadata

Release files for anycast-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 anycast-agent 0.1.2
File Size Uploaded Provenance
anycast_agent-0.1.2.tar.gz 7.8 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 15.9 kB

Release files / anycast_agent-0.1.2.tar.gz

Download URL anycast_agent-0.1.2.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0d89353a7f6714ef7272f7d7452ac21edba6cfa627459f1786e363b18fb15131
BLAKE2b-256 checksum
How to use checksums
71440ec33929e001b3bdd4f39a3c3f89ed72e988d4c3cb17e26ec523937b34bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / anycast_agent-0.1.2-py3-none-any.whl

Download URL anycast_agent-0.1.2-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
71f948110ea574db069bd4a28c0d625e1c05a92b4cfdfbee532ad6aa35fec95a
BLAKE2b-256 checksum
How to use checksums
f30bdee71f557bde0f9e34420ee9190155973bc87646c7a10f02ad1284f8654f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for anycast-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