-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
64 lines (52 loc) · 1.89 KB
/
basic_usage.py
File metadata and controls
64 lines (52 loc) · 1.89 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
"""Basic programmatic usage of the Cycles client."""
from runcycles import (
Action,
Amount,
CommitRequest,
CyclesClient,
CyclesConfig,
CyclesMetrics,
ReservationCreateRequest,
Subject,
Unit,
)
def main() -> None:
config = CyclesConfig(
base_url="http://localhost:7878",
api_key="your-api-key",
tenant="acme",
)
with CyclesClient(config) as client:
# Full reserve → execute → commit lifecycle
response = client.create_reservation(ReservationCreateRequest(
idempotency_key="req-001",
subject=Subject(tenant="acme", agent="support-bot"),
action=Action(kind="llm.completion", name="gpt-4"),
estimate=Amount(unit=Unit.USD_MICROCENTS, amount=500_000),
ttl_ms=30_000,
))
print(f"Reservation: success={response.is_success}, body={response.body}")
if not response.is_success:
print(f"Failed: {response.error_message}")
return
reservation_id = response.get_body_attribute("reservation_id")
print(f"Reserved: {reservation_id}")
# Simulate work (result would be used in a real application)
_ = "Generated response text"
# Commit actual usage
commit_response = client.commit_reservation(reservation_id, CommitRequest(
idempotency_key="commit-001",
actual=Amount(unit=Unit.USD_MICROCENTS, amount=420_000),
metrics=CyclesMetrics(
tokens_input=1200,
tokens_output=800,
latency_ms=150,
model_version="gpt-4-0613",
),
))
print(f"Commit: success={commit_response.is_success}, body={commit_response.body}")
# Query balances
balances = client.get_balances(tenant="acme")
print(f"Balances: {balances.body}")
if __name__ == "__main__":
main()