Expand description
Automatic lifecycle wrapper — the Rust equivalent of Python’s @cycles
decorator and TypeScript’s withCycles higher-order function.
with_cycles wraps an async closure with the full reserve → execute →
commit/release lifecycle. On success, it commits actual spend; on error,
it releases the reservation automatically.
§Example
use runcycles::{CyclesClient, with_cycles, WithCyclesConfig, models::*};
let client = CyclesClient::builder("key", "http://localhost:7878")
.tenant("acme")
.build();
// One-liner: reserve → execute → commit (like @cycles in Python)
let result = with_cycles(
&client,
WithCyclesConfig::new(Amount::tokens(1000))
.action("llm.completion", "gpt-4o")
.subject(Subject { tenant: Some("acme".into()), ..Default::default() }),
|ctx| async move {
// Access caps if needed
let _caps = ctx.caps;
// Call your LLM
let response = "hello world".to_string();
// Return (result, actual_cost)
Ok((response, Amount::tokens(42)))
},
).await?;
println!("LLM said: {}", result);Structs§
- Guard
Context - Snapshot of guard data passed to the
with_cyclesclosure. - With
Cycles Config - Configuration for
with_cycles.
Functions§
- with_
cycles - Execute an async function with automatic Cycles budget enforcement.