Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions blockrun_llm/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,15 +1200,16 @@ def _handle_payment_and_retry(
self._session_calls += 1
self._session_total_usd += cost_usd
self._last_call_cost = cost_usd
self._capture_settlement(retry_response)
settlement = self._capture_settlement(retry_response)

# Attach the real x402 charge (and on-chain settlement) to THIS response
# object so callers get a per-call, race-free cost — _last_settlement is
# consumed by _log_transaction below and _last_call_cost goes stale on
# the free path, so neither is safe to read after the call returns.
# object so callers get a per-call, race-free cost. Use the value
# _capture_settlement returns rather than re-reading self._last_settlement
# (shared state a concurrent call on the same client could overwrite),
# and a local cost_usd rather than self._last_call_cost which goes stale.
chat_response.cost_usd = cost_usd
if self._last_settlement:
chat_response.settlement = dict(self._last_settlement)
if settlement:
chat_response.settlement = dict(settlement)

# Save full response locally (cost log + response archive)
from .cache import save_to_cache
Expand Down Expand Up @@ -2768,14 +2769,14 @@ async def _handle_payment_and_retry(
else float(details.get("amount", 0)) / 1e6
)
self._last_call_cost = cost_usd
self._capture_settlement(retry_response)
settlement = self._capture_settlement(retry_response)

response_data = retry_response.json()
# Per-call real charge + settlement (see sync _handle_payment_and_retry).
chat_response = ChatResponse(**response_data)
chat_response.cost_usd = cost_usd
if self._last_settlement:
chat_response.settlement = dict(self._last_settlement)
if settlement:
chat_response.settlement = dict(settlement)
from .cache import save_to_cache

save_to_cache(
Expand Down
6 changes: 1 addition & 5 deletions tests/unit/test_image_parameter_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ def test_edit_accepts_all_valid_parameters():

try:
client.edit(
"Make it red",
image=data_uri,
model="google/nano-banana",
size="1024x1024",
n=1
"Make it red", image=data_uri, model="google/nano-banana", size="1024x1024", n=1
)
except TypeError as e:
if "Valid parameters are" in str(e):
Expand Down
Loading