Unattended AI agents and n8n automations fail in two expensive ways: prompt-length spikes and duplicate retries after 5xx timeouts.
Pre-dispatch hard cap (X-Max-Cost-Rub)
Pass X-Max-Cost-Rub in your standard OpenAI SDK headers. Before dispatching to the upstream provider, anyllm.space computes the upper bound in RUB. If it exceeds your cap, the gateway immediately returns HTTP 422 max_cost_exceeded with 0 RUB charged.
from openai import OpenAI
import os
client = OpenAI(
base_url="https://api.anyllm.space/v1",
api_key=os.environ["ANYLLM_API_KEY"],
)
res = client.chat.completions.create(
model="deepseek/v4-pro-0813",
messages=[{"role": "user", "content": "Summarize SKU #17"}],
extra_headers={
"X-Max-Cost-Rub": "0.40",
"X-Client-Reference": "order-17",
"Idempotency-Key": "order-17-v1",
},
)Safe recovery after timeouts
Every operation tracks execution_status and billing_status independently. On a network drop, inspect GET /v1/requests/{id} or retry with the exact same Idempotency-Key — no second charge is created.