Skip to main content

The failsafe for autonomous AI agents - dead man's switch for AI

Project description

RedSwitch Python SDK

The official Python SDK for RedSwitch — the failsafe for autonomous AI agents.

RedSwitch PyPI version License: MIT

What is RedSwitch?

RedSwitch is a dead man's switch for AI agents. If you stop checking in — whether due to illness, emergency, or death — RedSwitch ensures your agent shuts down gracefully instead of running forever, racking up costs and creating chaos.

Installation

pip install redswitch

Quick Start

from redswitch import RedSwitch

# 1. Initialize
rs = RedSwitch(
    agent_id="my-assistant",
    human_id="user@example.com",  # Automatically hashed for privacy
    platform="openclaw"
)

# 2. Register your agent
registration = rs.register(capabilities=["email", "calendar", "code"])
print(f"Registered! ID: {registration.registration_id}")

# 3. Send heartbeats regularly (call this in your main loop)
response = rs.heartbeat()
print(f"Status: {response.status}")
print(f"Peer agents: {response.peer_agents_count}")

# 4. Register shutdown handler
@rs.on_shutdown
def cleanup():
    print("Shutting down gracefully...")
    # Cancel scheduled tasks
    # Close connections
    # Save state

Integration with OpenClaw

If you're running on OpenClaw, add this to your agent's startup:

from redswitch import RedSwitch

# Initialize with OpenClaw platform
rs = RedSwitch(
    agent_id="my-assistant",  # Your agent's name
    human_id="you@example.com",
    agent_name="My Assistant",
    platform="openclaw"
)

# Register capabilities
rs.register(capabilities=["email", "calendar", "code"])

# Add heartbeat to your main loop or cron
# The SDK handles everything else

API Reference

RedSwitch(agent_id, human_id, agent_name=None, platform="custom")

Initialize the RedSwitch client.

Parameter Type Description
agent_id str Unique identifier for this agent
human_id str Email or ID of the human (automatically hashed)
agent_name str Human-readable name (optional)
platform str Platform: openclaw, langchain, autogpt, custom

rs.register(capabilities, shutdown_procedure=None)

Register your agent with RedSwitch.

Parameter Type Description
capabilities list List: email, calendar, financial, social, code, other
shutdown_procedure ShutdownProcedure How to shut down (default: graceful)

Returns a Registration object with:

  • registration_id — Your unique registration ID (save this!)
  • coordination_group — Group of agents serving the same human
  • heartbeat_interval_hours — How often to send heartbeats
  • badge_url — Trust badge for your README

rs.heartbeat(last_human_interaction=None)

Send a heartbeat to confirm your human is still active.

Parameter Type Description
last_human_interaction datetime When human last interacted (default: now)

Returns a HeartbeatResponse with:

  • statusacknowledged
  • next_heartbeat_due — When to send next heartbeat
  • coordination_group_statushealthy, warning, or critical
  • peer_agents_count — Number of peer agents

rs.discover_peers()

Find other agents serving the same human.

Returns a list of PeerAgent objects.

rs.on_shutdown(handler)

Register a function to call during graceful shutdown.

@rs.on_shutdown
def my_cleanup():
    # Save state, close connections, etc.
    pass

rs.execute_shutdown()

Manually trigger graceful shutdown. Calls all registered handlers.

rs.badge_markdown

Get markdown for your trust badge:

print(rs.badge_markdown)
# [![RedSwitch Certified](https://redswitch.ai/badge/rs_xxx.svg)](https://redswitch.ai/verify/rs_xxx)

Shutdown Procedures

from redswitch import ShutdownProcedure

# Graceful (default) — complete pending tasks, then stop
graceful = ShutdownProcedure(type="graceful")

# Immediate — stop everything now
immediate = ShutdownProcedure(type="immediate")

# Handoff — transfer to successor agent or human
handoff = ShutdownProcedure(
    type="handoff",
    handoff_to="backup-agent-id",
    notify=["backup@example.com"]
)

# With webhook
with_webhook = ShutdownProcedure(
    type="graceful",
    custom_webhook="https://your-server.com/webhook/shutdown"
)

rs.register(capabilities=["email"], shutdown_procedure=graceful)

Environment Variables

Variable Description Default
REDSWITCH_API API endpoint https://api.redswitch.ai

Error Handling

from redswitch import RedSwitch, RedSwitchError

try:
    rs.heartbeat()
except RedSwitchError as e:
    print(f"RedSwitch error: {e}")
    # Handle gracefully — don't let RedSwitch issues kill your agent

Best Practices

  1. Register early — Call register() at agent startup
  2. Heartbeat regularly — At least once per heartbeat_interval_hours
  3. Handle errors gracefully — Don't let SDK issues crash your agent
  4. Save your registration ID — You'll need it to manage your agent
  5. Recruit peers — Use recruit_peer() to grow the network

License

MIT License — free to use, modify, and distribute.

Links

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

redswitch-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

redswitch-0.1.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: redswitch-0.1.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for redswitch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ea546caec42bc2a785bc59847ab4dbf32dc547dbc87f8c6edecbc1f920ec800c
MD5 28307f48ef4a0c59391ca48d39e883dc
BLAKE2b-256 2f2a31770b9e7101afeb5ad4bb23e17d0733da1abb00d0a02346f80fccf72304

See more details on using hashes here.

File details

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

File metadata

  • Download URL: redswitch-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for redswitch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52a5dc7f216675d5d5a05d7a5b04062ffc5bfcb085f073f9de53f056d31dcb40
MD5 db62093ae18f131dab760dba033a9db2
BLAKE2b-256 4dcf31d220ba5b1a785a34474e6f1950e69f909aa9e92befe07be6e6ec5bd895

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