Documentation

Control API cost with keys, limits, and logs in the NexoRouter documentation.

Control API cost with keys, limits, and logs

Cost control starts before the first request. Use a dedicated key, a current model rate, bounded output, and Usage Logs to make spending visible.

Understand the billing unit

Open the model profile before integrating. Chat models commonly publish separate input and output rates per million tokens. Media models can use a different unit.

Do not compare models until their billing units match.

Create a limited production key

Use a separate API key for each application or environment. Configure:

  • A recognizable name.
  • A budget appropriate for the test or production period.
  • A model scope restricted to reviewed models.
  • An expiry date for temporary integrations.

Never expose a NexoRouter key in browser JavaScript, a mobile bundle, a public repository, a screenshot, or support chat.

Bound every request

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.NEXOROUTER_API_KEY,
  baseURL: "https://api.nexorouter.com/v1",
  timeout: 60_000,
  maxRetries: 2,
});

const response = await client.chat.completions.create({
  model: process.env.NEXOROUTER_MODEL,
  messages: [{ role: "user", content: "Return a five-item checklist." }],
  max_tokens: 300,
  temperature: 0.2,
});

console.log(response.usage);

Use max_tokens to cap output. Trim repeated history, reject oversized input, and limit concurrency per user.

Retries also consume capacity and can create duplicate work. Retry only transient errors, use backoff, respect retry-after, and set a maximum attempt count.

Read Usage Logs

After a test or incident, filter by:

  • API key.
  • Model ID.
  • Status.
  • Time window.

Review token counts, cost, latency, and request ID. Share the request ID—not the API key—when asking for support.

Add application-level guardrails

  1. Authenticate every end user.
  2. Apply per-user rate and concurrency limits.
  3. Reject prompts above your size policy.
  4. Set a request timeout.
  5. Store only the logs needed for operations.
  6. Alert on spend, error rate, and repeated 429, 502, or 504 responses.
  7. Stop traffic when the prepaid balance or key budget approaches your threshold.

Investigate unexpected spend

  1. Disable the affected key if misuse is active.
  2. Filter Usage Logs by key and time.
  3. Group by model and status.
  4. Check for long prompts, high max_tokens, retry loops, or leaked client-side keys.
  5. Replace the key and narrow its budget and model scope.
  6. Fix the application control before restoring traffic.
Control API cost with keys, limits, and logs — NexoRouter