Browse documentation

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/completions

Request Parameters

ParameterTypeRequiredDefaultDescription
modelstringYes—Model ID (e.g. gpt-5.4-mini, gpt-3.5-turbo-instruct).
promptstring or string[]Yes—Prompt text or array of prompt strings.
max_tokensintegerNo16Maximum number of tokens to generate.
temperaturenumberNo1.0Sampling temperature between 0.0 and 2.0.
top_pnumberNo1.0Nucleus sampling cutoff threshold.
nintegerNo1Number of completions to generate per prompt.
streambooleanNofalseStream tokens via Server-Sent Events (SSE).
logprobsintegerNonullReturn log probabilities for up to 5 most likely tokens.
echobooleanNofalseEcho prompt alongside completion.
stopstring or string[]NonullUp to 4 stop sequences.
presence_penaltynumberNo0Presence penalty between -2.0 and 2.0.
frequency_penaltynumberNo0Frequency penalty between -2.0 and 2.0.
nrouter_cachebooleanNotrueWhen false, bypasses response cache.
nrouter_prompt_template_idstringNo—Tenant-scoped managed prompt template UUID.
nrouter_prompt_variablesobjectNo—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 StatusError CodeReason
400 Bad Requestinvalid_requestMalformed JSON or invalid token bounds.
400 Bad Requestguardrail_blockedPreflight inspection detected prompt injection or policy violation.
401 Unauthorizedinvalid_api_keyVirtual key missing, expired, or invalid.
402 Payment Requiredinsufficient_creditsTenant credit balance insufficient for reservation hold.
404 Not Foundmodel_not_foundModel identifier not found or unauthorized for tenant.
429 Too Many Requestsrate_limit_exceededRPM/TPM ceiling crossed; check x-nr-limit-source.
500 / 503 Provider Errorservice_unavailableUpstream outage. Moves to fallback only if configured (nrouter_fallbacks).
POST
/v1/completions

Authorization

NRouterApiKey
AuthorizationBearer <token>

Your nRouter virtual key (sk-nrouter-…). Sent as Authorization: Bearer sk-nrouter-….

In: header

Header Parameters

x-nr-compress?string

Request prompt compression: on to compress eligible prompts, off to skip

Value in

  • "on"
  • "off"
x-nr-tags?string

Custom spend and attribution tags (comma-separated key=value pairs)

x-nr-mcp-server?string

Target MCP server ID when routing MCP tool calls or prompts through the gateway

x-nr-client-language?string

Caller SDK runtime language identification (e.g. typescript, python, rust, go)

x-nr-session-id?string

Caller session identifier allowed at edge for grouping related client turns

x-nr-trace?string

Inbound trace detail level override for request lifecycle spans

Value in

  • "off"
  • "stages"
  • "verbose"
x-nr-trace-stages?string

Inbound stage-level trace emission override

Value in

  • "off"
  • "stages"
  • "verbose"
x-nr-trace-id?string

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  }}
Was this page helpful?