Add comprehensive integration examples for Cycles Python client#9
Merged
Conversation
…nd streaming Five new runnable examples demonstrating real-world Cycles integration patterns: - openai_integration.py: budget-guarded chat completions with cost estimation - anthropic_integration.py: decorator + programmatic per-tool-call tracking - streaming_usage.py: reserve → stream → commit with release on failure - fastapi_integration.py: middleware, dependency injection, per-tenant isolation - langchain_integration.py: custom callback handler for budget-aware agents Also adds examples/README.md index and an Examples section to the project README. https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF
BudgetExceededError and CyclesProtocolError require `message` as the first positional argument. Extract fields from ErrorResponse correctly instead of passing an invalid `error_response=` parameter. https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF
…g comparison ErrorResponse.error_code is a property returning an ErrorCode enum. While str enum comparison to string literals works, using the raw .error string field is more idiomatic and avoids type confusion. https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a complete suite of runnable examples demonstrating how to integrate the
runcyclesclient into real-world Python applications. The examples cover decorator-based budget management, streaming workflows, and integrations with popular frameworks and LLM providers.Key Changes
New example files in
examples/directory:openai_integration.py– Guard OpenAI chat completions with budget checks, caps-aware token limiting, and cost trackinganthropic_integration.py– Protect Anthropic Messages API calls with per-tool-call budget tracking for agentic workflowsstreaming_usage.py– Demonstrate the reserve → stream → commit pattern for streaming responses with accurate cost calculationfastapi_integration.py– Show middleware-based preflight checks, dependency injection, per-tenant budget isolation, and exception handlinglangchain_integration.py– Custom callback handler that wraps each LLM call in a Cycles reservation for budget-aware agents and chainsREADME.md– Documentation and quick-start guide for all examplesUpdated main README.md – Added "Examples" section with a table linking to all example files and their descriptions
Notable Implementation Details
@cyclesdecorator for clean, declarative budget management with estimate/actual cost functionsBudgetExceededErrorandCyclesProtocolErrorwith appropriate HTTP status codesAll examples include clear documentation, environment variable configuration, and are ready to run with minimal setup.
https://claude.ai/code/session_019RuDwakEwtZDbEMYNvesUF