Errors

Errors

Every error is returned as JSON with a stable type you can branch on:

{ "error": { "type": "no_sources", "message": "provide sources, or set allow_ungrounded=true" } }

HTTP error table

HTTPtypeWhen
400no_sourcessources is empty and allowUngrounded is false.
400sources_too_largesources text exceeds the character budget (~100k).
400invalid_requestMalformed body — missing model/messages, bad JSON.
401invalid_keyMissing or rejected API key.
402insufficient_quotaBilling / quota exhausted (from the gateway).
429rate_limitedRate limit hit (from the gateway).
502extraction_failedThe model returned an unparseable structure after a retry.
5xxgateway_errorUpstream gateway/model failure.

Key validation and quota/rate limits are enforced by the gateway and surfaced here with the matching status, so you handle them the same way you handle any gateway error today.

Typed errors in the SDK

The SDK throws typed, catchable errors. Branch on the class or on .type / .status:

import {
  MaxModel,
  MaxModelError,
  AuthError,        // 401
  QuotaError,       // 402
  RateLimitError,   // 429
  NoSourcesError,   // 400 no_sources
  ExtractionError,  // 502 extraction_failed
  GatewayError,     // 5xx / network
} from 'maxmodel'
 
const mx = new MaxModel({ apiKey: process.env.MAXMODEL_KEY! })
 
try {
  const out = await mx.verified.create({ /* ... */ } as any)
} catch (e) {
  if (e instanceof NoSourcesError) {
    // you forgot sources, or set allowUngrounded
  } else if (e instanceof RateLimitError) {
    // back off and retry
  } else if (e instanceof MaxModelError) {
    console.error(e.type, e.status, e.message)
  } else {
    throw e
  }
}

Every SDK error extends MaxModelError, which carries:

class MaxModelError extends Error {
  type: string   // e.g. 'invalid_key'
  status: number // HTTP status, or 0 for client/network errors
}