Completions
Create raw text completions using legacy prompt models with streaming support, token usage tracking, automated failover, and multi-tenant budget controls.
The /v1/completions endpoint provides raw text completion for base models, instruction-tuned models, and legacy completion workloads across provider clouds at exact list price with zero percent (0%) markup. Modern conversational applications typically use Chat Completions, while Completions remains available for code synthesis, text infilling, and deterministic single-prompt tasks.
Preflight evaluates rate limits (RPM/TPM), guardrails, and credit reservations before dispatch (x-nr-guardrails: blocked returns HTTP 400 with $0 spent and $0 held). Emits latency, cost, routing, and cache headers (x-nr-response-cache).
POST https://api.nrouter.ai/v1/completionsRequest Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
model | string | Yes | — | Model ID (e.g. gpt-5.4-mini, gpt-3.5-turbo-instruct). |
prompt | string or string[] | Yes | — | Prompt text or array of prompt strings. |
max_tokens | integer | No | 16 | Maximum number of tokens to generate. |
temperature | number | No | 1.0 | Sampling temperature between 0.0 and 2.0. |
top_p | number | No | 1.0 | Nucleus sampling cutoff threshold. |
n | integer | No | 1 | Number of completions to generate per prompt. |
stream | boolean | No | false | Stream tokens via Server-Sent Events (SSE). |
logprobs | integer | No | null | Return log probabilities for up to 5 most likely tokens. |
echo | boolean | No | false | Echo prompt alongside completion. |
stop | string or string[] | No | null | Up to 4 stop sequences. |
presence_penalty | number | No | 0 | Presence penalty between -2.0 and 2.0. |
frequency_penalty | number | No | 0 | Frequency penalty between -2.0 and 2.0. |
nrouter_cache | boolean | No | true | When false, bypasses response cache. |
nrouter_prompt_template_id | string | No | — | Tenant-scoped managed prompt template UUID. |
nrouter_prompt_variables | object | No | — | Key-value variables interpolated into template. |
Examples
import { nRouter } from "@nrouter_ai/sdk";
const client = new nRouter({ apiKey: process.env.NROUTER_API_KEY });
const res = await client.completions.create({
model: "gpt-5.4-mini",
prompt: "Write a Rust hash function.",
max_tokens: 128,
});
console.log(res.choices[0].text, client.lastResponse?.cost);Headers & Errors
Required request headers: Authorization: Bearer sk-nrouter-... and Content-Type: application/json. Optional: x-nr-tags, x-nr-compress. Telemetry: x-nr-request-id, x-nr-latency-ms, x-nr-request-cost, x-nr-cost-status, x-nr-model, x-nr-routing, x-nr-attempts, x-nr-guardrails, x-nr-response-cache. See Chat Completions.
| HTTP Status | Error Code | Reason |
|---|---|---|
| 400 Bad Request | invalid_request | Malformed JSON or invalid token bounds. |
| 400 Bad Request | guardrail_blocked | Preflight inspection detected prompt injection or policy violation. |
| 401 Unauthorized | invalid_api_key | Virtual key missing, expired, or invalid. |
| 402 Payment Required | insufficient_credits | Tenant credit balance insufficient for reservation hold. |
| 404 Not Found | model_not_found | Model identifier not found or unauthorized for tenant. |
| 429 Too Many Requests | rate_limit_exceeded | RPM/TPM ceiling crossed; check x-nr-limit-source. |
| 500 / 503 Provider Error | service_unavailable | Upstream outage. Moves to fallback only if configured (nrouter_fallbacks). |
Authorization
NRouterApiKey Your nRouter virtual key (sk-nrouter-…). Sent as Authorization: Bearer sk-nrouter-….
In: header
Header Parameters
Request prompt compression: on to compress eligible prompts, off to skip
Value in
- "on"
- "off"
Target MCP server ID when routing MCP tool calls or prompts through the gateway
Caller SDK runtime language identification (e.g. typescript, python, rust, go)
Caller session identifier allowed at edge for grouping related client turns
Inbound trace detail level override for request lifecycle spans
Value in
- "off"
- "stages"
- "verbose"
Inbound stage-level trace emission override
Value in
- "off"
- "stages"
- "verbose"
Accepted so a client sending it is not refused; the value is not recorded and not propagated. Every response carries the gateway's own trace ID in this header, never the one sent.
Request Body
application/json
TypeScript Definitions
Use the request body type in TypeScript.
Response Body
application/json
application/json
application/json
application/json
application/json
application/json
application/json
application/json
application/json
curl -X POST "https://example.com/v1/completions" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-5.4-mini", "prompt": "Write a haiku about cloud infrastructure.", "max_tokens": 64 }'{ "id": "cmpl-abc123", "object": "text_completion", "model": "gpt-4o-mini", "choices": [ { "text": " there!", "index": 0, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 2, "completion_tokens": 2, "total_tokens": 4 }}