Skip to main content

Python SDK for Anycast Agents P2P connectivity

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anycast_agent-0.1.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anycast_agent-0.1.2-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file anycast_agent-0.1.2.tar.gz.

File metadata

  • Download URL: anycast_agent-0.1.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for anycast_agent-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0d89353a7f6714ef7272f7d7452ac21edba6cfa627459f1786e363b18fb15131
MD5 40cc882d50c8717e9c8c3d64493de7e5
BLAKE2b-256 71440ec33929e001b3bdd4f39a3c3f89ed72e988d4c3cb17e26ec523937b34bf

See more details on using hashes here.

File details

Details for the file anycast_agent-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: anycast_agent-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for anycast_agent-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 71f948110ea574db069bd4a28c0d625e1c05a92b4cfdfbee532ad6aa35fec95a
MD5 09d6dd5b3325d1356160967942536dea
BLAKE2b-256 f30bdee71f557bde0f9e34420ee9190155973bc87646c7a10f02ad1284f8654f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page