OpenAI Integration

Get started with the Memcode Router.

Use the official openai SDK and point its baseURL at the gateway. Requests use your Memcode Router key, and metered usage is tracked against its budget.

Setup

Three steps to your first token.

No new SDK, no new mental model. Change two fields in the client you already use and every request is metered, budgeted, and rate limited.

01

Point the SDK at the gateway

Set this value as the SDK's baseURL. Server-side only: never ship a router key in frontend JavaScript.

https://api.memcode.in/v1
02

Call the Responses API

Authenticate with your sm-* router key and set an explicit output cap. Without one, the router reserves budget for 4,096 output tokens.

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.MEMCODE_API_KEY,
  baseURL: "https://api.memcode.in/v1",
});

const response = await client.responses.create({
  model: "gpt-5.6-terra",
  input: "Explain quantum computing in simple terms.",
  max_output_tokens: 256,
});

console.log(response.output_text);
03

Pick a model

Choose a supported model ID from your key's provider. Featured models are listed below with base ledger rates per 1M text tokens; cache-write and long-context premiums are detailed in the docs.

ModelInputCachedOutput
gpt-5.6-terraOpenAIBalanced
Balanced tier for premium production workloads
$2.00$0.20$12.00
gpt-5.6-lunaOpenAIFast
Low-cost tier for fast everyday usage
$0.20$0.02$1.20
gemini-3.1-pro-previewGeminiFlagship
Frontier reasoning with long-context support
$2.00$0.20$12.00
gemini-3.7-flashGeminiBalanced
Strong quality at fast-tier latency
$0.75$0.075$3.75
gemini-3.1-flash-liteGeminiBudget
Lowest-cost tier for high-volume usage
$0.25$0.025$1.50
claude-opus-4-8AnthropicFlagship
Frontier reasoning, coding, and agentic work
$5.00$0.50$25.00
claude-sonnet-4-6AnthropicBalanced
Balanced Claude tier for production workloads
$3.00$0.30$15.00

Cache-write and long-context multipliers apply to some models. See the pricing details.

Budget Control

Every token is accounted for.

Before a request reaches OpenAI, the router atomically reserves its worst-case cost against your key's budget, then settles the real charge from returned usage. Check the used and remaining balance any time.