Chat Completion

Run writing, reasoning, extraction, JSON, and HTML tasks with a selected model
View as Markdown

Runs an OpenAI-compatible message list against a selected chat model.

POST /domains/models/capabilities/chat-complete/execute

Call List All Models first. The exact returned models[].id becomes this endpoint’s model value.

Request

import { AgentRouterClient } from "@agentrouter/agentrouter";
const client = new AgentRouterClient({
apiKey: process.env.AGENTIC_API_KEY,
});
const result = await client.models.chat.complete.execute(
{
model: "openai/gpt-4.1-nano",
messages: [
{
role: "system",
content: "You write concise landing-page copy.",
},
{
role: "user",
content: "Write a one-line hero for an AI course builder.",
},
],
max_tokens: 120,
temperature: 0.2,
},
{ allowFallback: true },
);
console.log(result.completionText);
console.log(result.creditsCharged);

Capability input

FieldTypeRequiredDescription
modelstringyesExact live model ID. For OpenRouter, use an ID returned by List All Models.
messagesobject[]yesOpenAI-compatible messages with role and content.
max_tokensintegernoMaximum generated tokens. Must be at least 1.
temperaturenumbernoSampling temperature from 0 to 2.
top_pnumbernoNucleus sampling value from 0 to 1.
stopstringnoStop sequence. Support can vary by provider and model.
streambooleannoRequest streaming when the selected route supports it.

Check the selected catalog entry’s supported_parameters before sending optional model fields.

Routing controls

Routing controls are the SDK method’s second argument. With raw HTTP they are fields in the same JSON body.

FieldTypeRequiredDescription
routeKeystringnoPin a route such as models.chat.complete.openrouter.mpp.
providerstringnoProvider hint such as openrouter, deepseek, or groq.
allowFallbackbooleannoLet AgentRouter use another compatible route when the preferred route fails.

Response

{
"success": true,
"completionText": "Build your course in minutes, not months.",
"model": "openai/gpt-4.1-nano",
"provider": "openrouter",
"routeKey": "models.chat.complete.openrouter.mpp",
"fallbackUsed": false,
"finishReason": "stop",
"usage": {
"promptTokens": 31,
"completionTokens": 10,
"totalTokens": 41
},
"creditsCharged": 1
}

The token counts and charge above are illustrative. Always use the values returned by the live response.

Response fields

FieldTypeDescription
completionTextstringGenerated text. This is the primary result.
modelstringModel that handled the request.
providerstringProvider selected for execution.
routeKeystringConcrete route used.
fallbackUsedbooleanWhether AgentRouter moved to a fallback route.
finishReasonstringWhy generation stopped when published by the provider.
usageobjectProvider token usage when available.
creditsChargednumberAuthoritative credits debited for this request.

Common scenarios

The endpoint always performs model inference. Your prompt and messages define the job.

ScenarioInput pattern
Sales pageSystem message sets voice and structure; user message contains offer, audience, proof, and CTA.
Course planAsk for a JSON schema or structured outline in the user message.
HTMLAsk for HTML/CSS and state whether markdown fences are allowed.
Email draftProvide recipient context and desired action. This drafts text; it does not send email.
ExtractionState the output schema and ask for JSON only.

Pricing

Catalog rates are estimates per one million input and output tokens. The paid MPP route obtains a request-specific quote. creditsCharged is the final amount; do not calculate the debit from the catalog alone.

Inspect the exact route contract

const context = await client.catalog.routes.context(
"models.chat.complete.openrouter.mpp",
);
console.log(context.execute?.requiredFields);
console.log(context.execute?.optionalFields);
console.log(context.execute?.fields);

The capability-level contract is currently partial. Route context is the machine-readable source of truth for a pinned model route.

Errors

StatusMeaningFix
400 or 422Missing field, invalid parameter, or unsupported parameterVerify model, messages, and the route’s context.
401Missing or invalid AgentRouter API keySend Authorization: Bearer aak_....
402Insufficient AgentRouter credits or payment-route fundingAdd funds, then retry the same request.
404Model or pinned route is unavailableRefresh List All Models or remove the stale routeKey.
429Provider or gateway rate limitRetry with exponential backoff.