Tolka Edge SymbolTolka Edge WordmarkDocs

Error Handling

Build resilient integrations against Tolka's structured error envelope, with per-code handling strategies.

Tolka returns a structured, OpenAI-compatible error envelope so your code can branch on stable machine-readable codes.

The envelope

{
  "error": {
    "message": "Insufficient balance to route request.",
    "type": "payment_required",
    "code": "insufficient_balance",
    "param": "wallet",
    "tolka_details": { "balance_paise": 340, "required_paise_for_model": 400 }
  }
}
Always branch on error.code — messages may be reworded, codes are stable.

Per-code strategy

codeStatusStrategy
validation_error400Fix the request. Not retryable.
auth_error401Check the key. Not retryable.
insufficient_balance402Prompt a top-up; halt until funded.
upi_verification_required403Route the user to verification.
model_not_found404Correct the slug; list /v1/models.
request_timeout408The dedicated vLLM queue is full. Back off and retry.
internal_error500Retry with backoff; alert if persistent.

A robust handler

from openai import APIStatusError
 
def call_with_handling(**kwargs):
    try:
        return client.chat.completions.create(**kwargs)
    except APIStatusError as e:
        err = e.response.json().get("error", {})
        code = err.get("code")
        if code == "insufficient_balance":
            raise NeedsTopUp(err["tolka_details"])
        if code in ("request_timeout", "internal_error"):
            raise Retryable(code)
        raise  # validation/auth: surface immediately

Streaming errors

A stream that fails mid-flight ends with a final frame:
data: {"error": "stream_interrupted"}
Interrupted streams are billed for tokens delivered and logged as partial. Preserve the partial output and, if needed, retry only the remainder.
🔁

Retry strategy

Exponential backoff, jitter and idempotency.