GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cycles-client-python/examples/anthropic_integration.py at main · runcycles/cycles-client-python Expand file tree/ Copy pathanthropic_integration.pyMore file actions 236 lines (201 loc) · 7.73 KB / Copy pathanthropic_integration.pyFile metadata and controls236 lines (201 loc) · 7.73 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 """Integrating Cycles with the Anthropic Python SDK. Guards Anthropic Messages API calls with budget reservations and demonstrates per-tool-call budget tracking for agentic workflows. Requirements: pip install runcycles anthropic Environment variables: CYCLES_BASE_URL - Cycles server URL (default: http://localhost:7878) CYCLES_API_KEY - Cycles API key CYCLES_TENANT - Tenant identifier ANTHROPIC_API_KEY - Anthropic API key """ import os import uuid from anthropic import Anthropic from runcycles import ( Action, Amount, BudgetExceededError, CommitRequest, CyclesClient, CyclesConfig, CyclesMetrics, ReservationCreateRequest, Subject, Unit, cycles, get_cycles_context, set_default_client, ) # --------------------------------------------------------------------------- # 1. Configure clients # --------------------------------------------------------------------------- config = CyclesConfig( base_url=os.environ.get("CYCLES_BASE_URL", "http://localhost:7878"), api_key=os.environ.get("CYCLES_API_KEY", "your-api-key"), tenant=os.environ.get("CYCLES_TENANT", "acme"), app="anthropic-example", ) cycles_client = CyclesClient(config) set_default_client(cycles_client) anthropic_client = Anthropic() # reads ANTHROPIC_API_KEY from env # Pricing in USD microcents (1 USD = 100_000_000 microcents). PRICE_PER_INPUT_TOKEN = 300 # $3.00 / 1M tokens PRICE_PER_OUTPUT_TOKEN = 1_500 # $15.00 / 1M tokens # --------------------------------------------------------------------------- # 2. Simple decorator-based integration # --------------------------------------------------------------------------- @cycles( estimate=lambda prompt, **kw: ( len(prompt.split()) * 2 * PRICE_PER_INPUT_TOKEN + kw.get("max_tokens", 1024) * PRICE_PER_OUTPUT_TOKEN ), actual=lambda result: ( result["usage"]["input_tokens"] * PRICE_PER_INPUT_TOKEN + result["usage"]["output_tokens"] * PRICE_PER_OUTPUT_TOKEN ), action_kind="llm.completion", action_name="claude-sonnet-4-20250514", unit="USD_MICROCENTS", ttl_ms=60_000, ) def send_message(prompt: str, max_tokens: int = 1024) -> dict: """Send a message to Claude with budget protection.""" ctx = get_cycles_context() # Respect caps if ctx and ctx.has_caps() and ctx.caps.max_tokens: max_tokens = min(max_tokens, ctx.caps.max_tokens) response = anthropic_client.messages.create( model="claude-sonnet-4-20250514", max_tokens=max_tokens, messages=[{"role": "user", "content": prompt}], ) if ctx: ctx.metrics = CyclesMetrics( tokens_input=response.usage.input_tokens, tokens_output=response.usage.output_tokens, model_version=response.model, ) return { "content": response.content[0].text, "usage": { "input_tokens": response.usage.input_tokens, "output_tokens": response.usage.output_tokens, }, } # --------------------------------------------------------------------------- # 3. Per-tool-call budget tracking (programmatic) # --------------------------------------------------------------------------- TOOLS = [ { "name": "get_weather", "description": "Get current weather for a location.", "input_schema": { "type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"], }, } ] def handle_tool_call(tool_name: str, tool_input: dict) -> str: """Simulate executing a tool.""" if tool_name == "get_weather": return f"72°F and sunny in {tool_input.get('location', 'unknown')}" return "Unknown tool" def chat_with_tools(prompt: str) -> str: """Multi-turn conversation with tool use, each LLM call budget-guarded.""" messages = [{"role": "user", "content": prompt}] turn = 0 while True: turn += 1 idempotency_key = f"tool-chat-{uuid.uuid4()}" # Reserve budget for this turn reserve_response = cycles_client.create_reservation( ReservationCreateRequest( idempotency_key=idempotency_key, subject=Subject( tenant=config.tenant, agent="tool-agent", toolset="weather-tools", ), action=Action( kind="llm.completion", name="claude-sonnet-4-20250514", tags=[f"turn-{turn}"], ), estimate=Amount(unit=Unit.USD_MICROCENTS, amount=2_000_000), ttl_ms=30_000, ) ) if not reserve_response.is_success: error_resp = reserve_response.get_error_response() if error_resp and error_resp.error == "BUDGET_EXCEEDED": return "Budget exhausted — cannot continue conversation." return f"Reservation failed: {reserve_response.error_message}" reservation_id = reserve_response.get_body_attribute("reservation_id") # Call Claude response = anthropic_client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, tools=TOOLS, messages=messages, ) # Commit actual cost actual_cost = ( response.usage.input_tokens * PRICE_PER_INPUT_TOKEN + response.usage.output_tokens * PRICE_PER_OUTPUT_TOKEN ) cycles_client.commit_reservation( reservation_id, CommitRequest( idempotency_key=f"commit-{idempotency_key}", actual=Amount(unit=Unit.USD_MICROCENTS, amount=actual_cost), metrics=CyclesMetrics( tokens_input=response.usage.input_tokens, tokens_output=response.usage.output_tokens, model_version=response.model, custom={"turn": turn, "tool_use": response.stop_reason == "tool_use"}, ), ), ) # Process response if response.stop_reason == "end_turn": # Final text response for block in response.content: if block.type == "text": return block.text return "" if response.stop_reason == "tool_use": # Extract tool calls and execute them messages.append({"role": "assistant", "content": response.content}) tool_results = [] for block in response.content: if block.type == "tool_use": result = handle_tool_call(block.name, block.input) tool_results.append({ "type": "tool_result", "tool_use_id": block.id, "content": result, }) messages.append({"role": "user", "content": tool_results}) else: # Unexpected stop reason return f"Stopped with reason: {response.stop_reason}" if turn >= 5: return "Maximum turns reached." # --------------------------------------------------------------------------- # 4. Run it # --------------------------------------------------------------------------- def main() -> None: print("=== Simple message ===") try: result = send_message("What is budget authority in one sentence?") print(f"Response: {result['content']}") print(f"Tokens: {result['usage']}") except BudgetExceededError: print("Budget exhausted.") print("\n=== Tool-use conversation ===") answer = chat_with_tools("What's the weather like in San Francisco?") print(f"Answer: {answer}") if __name__ == "__main__": main() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |