Skip to main content

AI API

Endpoints for interacting with AI models through a unified provider layer. These endpoints are served by the backend API service, not the web application. They are available at the backend base URL, which may differ from the web API base URL depending on your deployment.
These endpoints do not require session authentication. They are internal backend endpoints and are not exposed through the web application’s /api routes.

Health check

GET /api/ai/health
Returns the availability status of configured AI providers.

Response

{
  "status": "healthy",
  "providers": {
    "openrouter": true
  },
  "timestamp": "2026-03-19T00:00:00Z"
}
The status field is healthy when all providers are reachable and degraded when one or more are down.

Error response

When one or more providers fail, the response uses status: "error" and includes the error message:
{
  "status": "error",
  "error": "Provider connection failed"
}
CodeDescription
503AI service unavailable

List models

GET /api/ai/models
Returns all available AI models across providers.

Response

{
  "models": [
    {
      "id": "anthropic/claude-sonnet-4-20250514",
      "provider": "openrouter"
    }
  ],
  "count": 1,
  "openrouter": 1,
  "timestamp": "2026-03-19T00:00:00Z"
}

Errors

CodeDescription
500Failed to fetch models

List models by provider

GET /api/ai/models/:provider

Path parameters

ParameterTypeDescription
providerstringProvider name (for example, openrouter)

Response

{
  "provider": "openrouter",
  "models": [],
  "count": 0,
  "timestamp": "2026-03-19T00:00:00Z"
}

Select model

POST /api/ai/models/select
Automatically selects the best model for a given task type.

Request body

FieldTypeRequiredDescription
taskTypestringNoType of task (default: general)

Response

{
  "model": {
    "id": "anthropic/claude-sonnet-4-20250514",
    "provider": "openrouter"
  },
  "taskType": "general",
  "timestamp": "2026-03-19T00:00:00Z"
}

Errors

CodeDescription
404No models available

Chat completion

POST /api/ai/chat
Send a chat completion request through the unified AI provider layer. The model is auto-selected if not specified.

Request body

FieldTypeRequiredDescription
messagesarrayYesArray of message objects with role (user, assistant, or system) and content
modelstringNoModel ID. Auto-selected based on taskType if omitted.
taskTypestringNoUsed for auto-selection when model is omitted
temperaturenumberNoSampling temperature
top_pnumberNoNucleus sampling parameter
max_tokensnumberNoMaximum tokens in the response

Example request

{
  "messages": [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Hello!" }
  ],
  "temperature": 0.7,
  "max_tokens": 1024
}

Response

Returns the AI provider’s chat completion response. The exact shape depends on the provider used.

Errors

CodeDescription
400Messages array is required and must be non-empty
404No models available
500AI provider error

Estimate cost

POST /api/ai/estimate-cost
Estimate the cost of a request based on token counts and model pricing.

Request body

FieldTypeRequiredDescription
modelstringYesModel ID
inputTokensnumberYesNumber of input tokens
outputTokensnumberYesNumber of output tokens

Response

{
  "model": "anthropic/claude-sonnet-4-20250514",
  "inputTokens": 1000,
  "outputTokens": 500,
  "estimatedCost": 0.0045,
  "currency": "USD",
  "timestamp": "2026-03-19T00:00:00Z"
}

Errors

CodeDescription
400Model, inputTokens, and outputTokens are all required