-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlangchain_integration.py
More file actions
279 lines (233 loc) · 9.26 KB
/
langchain_integration.py
File metadata and controls
279 lines (233 loc) · 9.26 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""Integrating Cycles with LangChain via the BaseCallbackHandler API.
This example uses LangChain's traditional callback-handler pattern to wrap each
LLM call with a Cycles reservation. It is the right fit for **non-agent**
LangChain workflows — bare ``ChatOpenAI``/``ChatAnthropic`` runnables, chains,
RAG pipelines, etc.
For LangChain **agents** built with ``langchain.agents.create_agent`` (the
``wrap_tool_call`` / ``before_model`` middleware API introduced in LangChain
1.x), use the dedicated middleware package instead:
pip install langchain-runcycles
# https://github.com/runcycles/langchain-runcycles
That package exposes ``CyclesToolGate`` (pre-tool-call authorization) and
``CyclesFanOutGate`` (turn-cap / fan-out halts) — both work with sync and
async agents and offer features the callback handler cannot, such as denying
a tool call before it runs and halting an agent loop on remote policy.
Requirements:
pip install runcycles langchain langchain-openai
Environment variables:
CYCLES_BASE_URL - Cycles server URL (default: http://localhost:7878)
CYCLES_API_KEY - Cycles API key
CYCLES_TENANT - Tenant identifier
OPENAI_API_KEY - OpenAI API key
"""
from __future__ import annotations
import os
import uuid
from typing import Any
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.messages import HumanMessage
from langchain_core.outputs import LLMResult
from langchain_openai import ChatOpenAI
from runcycles import (
Action,
Amount,
BudgetExceededError,
CommitRequest,
CyclesClient,
CyclesConfig,
CyclesMetrics,
CyclesProtocolError,
ReleaseRequest,
ReservationCreateRequest,
Subject,
Unit,
)
# Pricing in USD microcents
PRICE_PER_INPUT_TOKEN = 250
PRICE_PER_OUTPUT_TOKEN = 1_000
# ---------------------------------------------------------------------------
# 1. Custom Callback Handler
# ---------------------------------------------------------------------------
class CyclesBudgetHandler(BaseCallbackHandler):
"""LangChain callback handler that wraps each LLM call with a Cycles reservation.
Usage:
handler = CyclesBudgetHandler(client, subject=Subject(tenant="acme"))
llm = ChatOpenAI(callbacks=[handler])
"""
def __init__(
self,
client: CyclesClient,
subject: Subject,
estimate_amount: int = 2_000_000,
action_kind: str = "llm.completion",
action_name: str = "gpt-4o",
) -> None:
super().__init__()
self.client = client
self.subject = subject
self.estimate_amount = estimate_amount
self.action_kind = action_kind
self.action_name = action_name
# Track active reservations by run_id
self._reservations: dict[str, str] = {}
self._idempotency_keys: dict[str, str] = {}
def on_llm_start(
self,
serialized: dict[str, Any],
prompts: list[str],
*,
run_id: uuid.UUID,
**kwargs: Any,
) -> None:
"""Create a budget reservation before each LLM call."""
key = str(uuid.uuid4())
self._idempotency_keys[str(run_id)] = key
response = self.client.create_reservation(
ReservationCreateRequest(
idempotency_key=key,
subject=self.subject,
action=Action(kind=self.action_kind, name=self.action_name),
estimate=Amount(unit=Unit.USD_MICROCENTS, amount=self.estimate_amount),
ttl_ms=60_000,
)
)
if not response.is_success:
error = response.get_error_response()
if error and error.error == "BUDGET_EXCEEDED":
raise BudgetExceededError(
error.message,
status=response.status,
error_code=error.error,
request_id=error.request_id,
details=error.details,
)
msg = error.message if error else (response.error_message or "Reservation failed")
raise CyclesProtocolError(
msg,
status=response.status,
error_code=error.error if error else None,
request_id=error.request_id if error else None,
details=error.details if error else None,
)
reservation_id = response.get_body_attribute("reservation_id")
self._reservations[str(run_id)] = reservation_id
def on_llm_end(
self,
response: LLMResult,
*,
run_id: uuid.UUID,
**kwargs: Any,
) -> None:
"""Commit actual cost after the LLM call completes."""
run_key = str(run_id)
reservation_id = self._reservations.pop(run_key, None)
idempotency_key = self._idempotency_keys.pop(run_key, None)
if not reservation_id or not idempotency_key:
return
# Extract token usage from LangChain's response
token_usage = (response.llm_output or {}).get("token_usage", {})
input_tokens = token_usage.get("prompt_tokens", 0)
output_tokens = token_usage.get("completion_tokens", 0)
actual_cost = (
input_tokens * PRICE_PER_INPUT_TOKEN
+ output_tokens * PRICE_PER_OUTPUT_TOKEN
)
self.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=input_tokens,
tokens_output=output_tokens,
model_version=token_usage.get("model_name", self.action_name),
),
),
)
def on_llm_error(
self,
error: BaseException,
*,
run_id: uuid.UUID,
**kwargs: Any,
) -> None:
"""Release the reservation if the LLM call fails."""
run_key = str(run_id)
reservation_id = self._reservations.pop(run_key, None)
idempotency_key = self._idempotency_keys.pop(run_key, None)
if reservation_id and idempotency_key:
self.client.release_reservation(
reservation_id,
ReleaseRequest(idempotency_key=f"release-{idempotency_key}"),
)
# ---------------------------------------------------------------------------
# 2. Using the handler with a chat model
# ---------------------------------------------------------------------------
def simple_chain_example() -> None:
"""Run a simple LangChain invocation with budget protection."""
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"),
)
client = CyclesClient(config)
handler = CyclesBudgetHandler(
client=client,
subject=Subject(tenant=config.tenant, agent="langchain-agent"),
)
llm = ChatOpenAI(
model="gpt-4o",
callbacks=[handler],
)
print("=== Simple invocation ===")
try:
result = llm.invoke([HumanMessage(content="What is budget authority in one sentence?")])
print(f"Response: {result.content}")
except BudgetExceededError:
print("Budget exhausted — cannot invoke LLM.")
client.close()
# ---------------------------------------------------------------------------
# 3. Using with an agent that has tools
# ---------------------------------------------------------------------------
def agent_with_tools_example() -> None:
"""Run a LangChain agent with tools, each LLM call budget-guarded."""
from langchain_core.tools import tool
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"),
)
client = CyclesClient(config)
handler = CyclesBudgetHandler(
client=client,
subject=Subject(tenant=config.tenant, agent="tool-agent", toolset="weather"),
)
@tool
def get_weather(location: str) -> str:
"""Get the current weather for a location."""
return f"72°F and sunny in {location}"
llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])
llm_with_tools = llm.bind_tools([get_weather])
print("\n=== Agent with tools ===")
try:
result = llm_with_tools.invoke(
[HumanMessage(content="What's the weather in San Francisco?")]
)
print(f"Response: {result.content}")
# If the model requested a tool call, show it
if result.tool_calls:
for tc in result.tool_calls:
print(f" Tool call: {tc['name']}({tc['args']})")
tool_result = get_weather.invoke(tc["args"])
print(f" Tool result: {tool_result}")
except BudgetExceededError:
print("Budget exhausted — agent stopped.")
client.close()
# ---------------------------------------------------------------------------
# 4. Run examples
# ---------------------------------------------------------------------------
def main() -> None:
simple_chain_example()
agent_with_tools_example()
if __name__ == "__main__":
main()