Skip to content

Add integration guides for OpenAI, Anthropic, LangChain, FastAPI, and streaming#30

Merged
amavashev merged 3 commits intomainfrom
claude/python-examples-analysis-5a0Ej
Mar 15, 2026
Merged

Add integration guides for OpenAI, Anthropic, LangChain, FastAPI, and streaming#30
amavashev merged 3 commits intomainfrom
claude/python-examples-analysis-5a0Ej

Conversation

@amavashev
Copy link
Contributor

Summary

This PR adds comprehensive how-to guides for integrating the Cycles budget management system with popular Python frameworks and LLM providers. These guides provide practical patterns for cost-controlling API calls across different use cases.

Changes

  • OpenAI Integration Guide (how-to/integrating-cycles-with-openai.md): Demonstrates the @cycles decorator pattern for guarding chat completions with budget reservations, cost estimation strategies, budget exhaustion handling, and metrics reporting.

  • Anthropic Integration Guide (how-to/integrating-cycles-with-anthropic.md): Shows both decorator-based simple calls and programmatic per-turn budget tracking for agentic workflows with tool use, including pricing reference table.

  • LangChain Integration Guide (how-to/integrating-cycles-with-langchain.md): Explains the callback handler approach (CyclesBudgetHandler) for wrapping every LLM call in LangChain applications, with examples for chat models and agents with tools.

  • FastAPI Integration Guide (how-to/integrating-cycles-with-fastapi.md): Covers client lifecycle management, exception handlers for budget errors, preflight middleware with decide(), per-tenant isolation, and budget dashboard endpoints.

  • Streaming Responses Guide (how-to/handling-streaming-responses-with-cycles.md): Addresses the reserve → stream → commit pattern for streaming LLM responses, TTL considerations, release-on-failure semantics, and cap respecting.

  • Documentation Navigation: Updated .vitepress/config.ts to include the new guides in the sidebar navigation under the how-to section.

Notable Implementation Details

  • Each guide includes runnable code examples with proper error handling and idempotency key management
  • Pricing tables and token cost calculations are provided for reference
  • Guides emphasize the distinction between decorator-based (simple, automatic) and programmatic (complex, manual) approaches
  • TTL and cap-respecting patterns are consistently demonstrated across all integrations
  • Links to full example scripts in the repository are included for each guide

https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF

claude added 3 commits March 15, 2026 11:28
…aming integrations

Five new how-to documentation pages with code examples and best practices:
- Integrating with OpenAI: cost estimation, caps, metrics
- Integrating with Anthropic: decorator + per-tool-call budget tracking
- Integrating with LangChain: custom callback handler pattern
- Integrating with FastAPI: middleware, DI, per-tenant isolation
- Handling Streaming Responses: reserve → stream → commit pattern

Updates VitePress sidebar to include the new pages.

https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF
- Remove tenant=lambda from @cycles decorator in FastAPI guide (tenant
  parameter only accepts str | None, not callables)
- Fix exception constructor calls in LangChain guide to pass message
  as first positional argument

https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF
@amavashev amavashev merged commit 173966b into main Mar 15, 2026
1 check passed
@amavashev amavashev deleted the claude/python-examples-analysis-5a0Ej branch March 17, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants