Cycles Python Client Examples

April 8, 2026 · View on GitHub

Runnable examples demonstrating how to integrate the runcycles client into real-world Python applications.

Prerequisites

  1. A running Cycles server (see Deploy the Full Stack)
  2. Set environment variables:
export CYCLES_BASE_URL="http://localhost:7878"
export CYCLES_API_KEY="your-api-key"   # create via Admin Server — see link above
export CYCLES_TENANT="acme"
  1. Install the client:
pip install runcycles

Examples

FileDescriptionExtra Dependencies
basic_usage.pyProgrammatic reserve → commit lifecycle
decorator_usage.py@cycles decorator with estimates, caps, and metrics
async_usage.pyAsync client and async decorator
openai_integration.pyGuard OpenAI chat completions with budget checksopenai
anthropic_integration.pyGuard Anthropic messages with per-tool budget trackinganthropic
streaming_usage.pystream_reservation() context manager with auto-commitopenai
fastapi_integration.pyFastAPI middleware, dependency injection, per-tenant budgetsfastapi, uvicorn
langchain_integration.pyLangChain callback handler for budget-aware agentslangchain, langchain-openai

Running

# Basic examples (only need a Cycles server)
python examples/basic_usage.py
python examples/decorator_usage.py
python examples/async_usage.py

# Integration examples (need additional API keys)
export OPENAI_API_KEY="sk-..."
python examples/openai_integration.py
python examples/streaming_usage.py

export ANTHROPIC_API_KEY="sk-ant-..."
python examples/anthropic_integration.py

# FastAPI (starts a server on port 8000)
pip install fastapi uvicorn
python examples/fastapi_integration.py

# LangChain
pip install langchain langchain-openai
python examples/langchain_integration.py