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.1.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.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anycast_agent-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 5e80d199848acef24390a41291b02a7285eefbd916a6a3c4fcac30fa5e835618
MD5 04cbec2c5f1ad93836ce9d167cdd7890
BLAKE2b-256 2c8ec6f393b2f6a7e4b915a15773ac61082ed3ac18dcf610e522369167216467

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anycast_agent-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b696385eccd35ef5dcbaeb15c8ff52587bab4659e8020e19fe117b267cbd5f20
MD5 5ebe088399a013582a604a52a42fc689
BLAKE2b-256 14afb3f2cb59c2973962a05ebf538d4bfb3c6072b07db92aba8e0ebf583aa627

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