Skip to main content

Overview

AI Sonar’s Agent-First API enriches error responses with structured hints that AI agents can parse and act on immediately — no web searches, no doc lookups, no guesswork. OpenAI-compatible Chat Completions and Responses gateway errors may include optional fields such as did_you_mean, suggestions, hint, retryable, and retry_after inside the error object. Anthropic Messages and Gemini endpoints keep their native error shapes and do not promise these extensions.

Error Hint Fields

For OpenAI-compatible gateway errors, all hint fields are optional extensions inside the error object:

Error Code Examples

model_not_found (400)

When a model name doesn’t match any active model:
The did_you_mean resolution uses:
  1. Static alias mapping (from production error data)
  2. Normalized string matching (strips hyphens, case-insensitive)
  3. Edit distance matching (threshold ≤ 3)
Public routes do not expose separate error codes for hidden, deferred, or non-public models. Treat unavailable public models the same way as a miss: inspect did_you_mean, suggestions, and hint, then retry with a supported public model.

insufficient_balance (402)

When account balance is too low for the estimated cost:
suggestions contains models cheaper than the estimated cost that the agent can switch to.

Temporary service errors (5xx)

When a model is temporarily unavailable, retry only when the response sets retryable to true:
When retryable is false, follow hint or switch to a model listed in alternatives.

rate_limit_exceeded (429)

The retry_after value is calculated from the actual rate limit window reset time.
OpenAI-compatible endpoints use the standard error envelope shown above. Anthropic-compatible and Gemini-compatible endpoints use their native response formats.

context_length_exceeded (400)

When input exceeds the model’s context window (with corrective hints):

Native Endpoint Discovery

Do not infer native protocol availability from a model or provider name, or from Chat response headers. Before choosing a native endpoint, read GET /v1/models/{model} and use only a format in aisonar.accepted_request_formats. Native requests use only the advertised protocol; temporary service availability is still decided at request time. An advertised request format controls endpoint availability; support for individual fields and tools remains service-specific.

/v1/models Enhancements

/v1/models now carries non-chat recommendation metadata that agents can use before they call image, video, music, 3D, TTS, STT, embedding, rerank, or translation endpoints.
When recommended_for is present, agent_preferences is derived from a cached 24-hour success-rate snapshot:
  • Window: 24 hours
  • Snapshot cache: stale-while-revalidate
  • status = "ready" means the model has enough recent samples to participate in ranking
  • status = "insufficient_samples" means the model stays visible but is not ranked ahead of scored models

Category Filtering

Recommendation Discovery

For non-chat workflows, agents should fetch the current recommended shortlist first:
Valid recommended_for values are:
  • image
  • video
  • music
  • 3d
  • tts
  • stt
  • embedding
  • rerank
  • translation
If both category and recommended_for are present, they must match exactly. Recommended agent flow:
  1. GET /v1/models?recommended_for=<scene>
  2. Pick the first agent_preferences.<scene>.status == "ready" model
  3. Call the endpoint explicitly with model=<selected>
  4. On transient errors only, retry with the next ready model

llms.txt

A machine-readable API overview is available at:
It includes:
  • First-call template with a working example
  • Common model names (dynamically generated from usage data)
  • All 12 API endpoints
  • Filter parameters for model discovery
  • Error handling guidance
AI agents that read llms.txt before their first API call can typically succeed on the first attempt.

Usage in Agent Code

Python (OpenAI SDK)

JavaScript (OpenAI SDK)

Design Principles

Fail fast, fail informatively

Errors return immediately with all the data an agent needs to self-correct.

No auto-routing

The API never silently substitutes a different model. The agent decides.

Data-driven suggestions

All recommendations come from production data, not hardcoded lists.

Backward compatible

All hint fields are optional. Existing clients see no difference.