SDK overview#
Write a request once, in the API you already use, and send it to a model from any provider by changing its model ID.
import { Router } from '@tokedex/sdk';
const router = new Router();
const response = await router.responses.create({
model: 'openai/gpt-6-luna',
input: 'Name the capital of France in one word.',
});
for (const item of response.output) {
if (item.type !== 'message') continue;
for (const part of item.content) if (part.type === 'output_text') console.log(part.text);
}from tokedex import Router
with Router() as router:
response = router.responses.create(
model="openai/gpt-6-luna", input="Name the capital of France in one word."
)
print(response.output_text)use tokedex::{Router, wire};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let router = Router::builder().build()?;
let request = wire::ResponsesRequest::new("openai/gpt-6-luna", "Name the capital of France in one word.");
let response = router.responses().create(request).await?;
println!("{}", response.output_text());
Ok(())
}import { Router } from '@tokedex/sdk';
const router = new Router();
const completion = await router.chat.completions.create({
model: 'openai/gpt-6-luna',
messages: [{ role: 'user', content: 'Name the capital of France in one word.' }],
});
console.log(completion.choices[0]?.message.content);from tokedex import Router
with Router() as router:
completion = router.chat.completions.create(
model="openai/gpt-6-luna",
messages=[{"role": "user", "content": "Name the capital of France in one word."}],
)
print(completion.choices[0].message.content)use tokedex::{Router, wire};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let router = Router::builder().build()?;
let messages = vec![wire::Message::user("Name the capital of France in one word.")];
let request = wire::ChatCompletionsRequest::new("openai/gpt-6-luna", messages);
let completion = router.chat().completions().create(request).await?;
if let Some(wire::Content::Text(text)) = &completion.choices[0].message.content {
println!("{text}");
}
Ok(())
}import { Router } from '@tokedex/sdk';
const router = new Router();
const message = await router.messages.create({
model: 'openai/gpt-6-luna',
messages: [{ role: 'user', content: 'Name the capital of France in one word.' }],
});
for (const block of message.content) if (block.type === 'text') console.log(block.text);from tokedex import Router
with Router() as router:
message = router.messages.create(
model="openai/gpt-6-luna",
messages=[{"role": "user", "content": "Name the capital of France in one word."}],
)
print("".join(block.text for block in message.content if block.type == "text"))use tokedex::{Router, wire};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let router = Router::builder().build()?;
let messages = vec![wire::Message::user("Name the capital of France in one word.")];
let request = wire::MessagesRequest::new("openai/gpt-6-luna", messages);
let message = router.messages().create(request).await?;
for block in &message.content {
if let wire::ContentBlock::Text { text, .. } = block {
println!("{text}");
}
}
Ok(())
}ParisThe model is OpenAI's, and the request and the reply are in OpenAI ResponsesOpenAI Chat CompletionsAnthropic Messages. It needs OPENAI_API_KEY in your environment, and the call is billed by OpenAI.
Run it with npx tsx hello.ts.
Behind the sample#
The Router sent the request to OpenAI and gave you the reply in OpenAI ResponsesOpenAI Chat CompletionsAnthropic Messages. openai/gpt-6-luna works the same in all three APIs, so the API you write in never decides the model. Your code reads every model's answer from the same fields and never sees a provider's format. To call another provider's model, change model: with anthropic/claude-haiku-5.5, the same request goes to Anthropic.
Most models are served on OpenAI Responses, OpenAI Chat Completions and Anthropic Messages. A few are served on one API only; router.models.list()router.models().list() returns one row for each model and API it serves.
The same Router also speaks a fourth API, Gemini GenerateContent, for Google's Gemini and Gemma models; see Generate text.
Keep the API your code already uses, since most models are served on all three and nothing needs porting. For new code, start with OpenAI Responses: it is the only API with the typed parseparse() helper and with conversation turns the Router stores for you. Choose Gemini GenerateContent only for code that calls Google's models alone, because it serves no other provider's.
Run the same code through a gateway#
const router = new Router({ baseUrl: 'http://127.0.0.1:4000', apiKey: process.env.TOKEDEX_API_KEY! });import os
from tokedex import Router
with Router(base_url="http://127.0.0.1:4000", api_key=os.environ["TOKEDEX_API_KEY"]) as router:
... # The same calls as before.use tokedex::{GatewayOptions, Router};
let router = Router::new(GatewayOptions::new("http://127.0.0.1:4000", std::env::var("TOKEDEX_API_KEY")?))?;Change the line that creates the Router and keep every other line. The gateway then holds the provider credentials, so your process needs only a Tokedex API key, and each call appears in the gateway's dashboard under Observability and Usage. Through a gateway, you can call the models that gateway serves, and its administrator chooses them. Run through a gateway covers headers and errors.
Move to a gateway once more than one application or person calls models on the same provider accounts: each then holds only its own API key, and you see every call in one dashboard.
Other Router features#
| To | Call | Guide |
|---|---|---|
| Stream the reply as it is written | .stream(…) on the same resource | Stream a reply |
| Get a typed, validated value | router.responses.parserouter.responses().parse | Structured output |
| Send images and files | Image and file parts in the request | Images and files |
| Continue a conversation | The API's own history fields | Conversations |
| Run a job at Batch prices | router.batches.createrouter.batches().create | Batch jobs |
| See which models and APIs you can call | router.models.list()router.models().list() | Choose a model |
To choose each provider and account yourself, or to add hooks and sinks, build your own router with the core package; see Routes and failover.
Install the SDK#
Run npm install @tokedex/sdkuv add tokedexcargo add tokedex, or follow the SDK quickstart.
The samples also need Tokio: cargo add tokio --features macros,rt-multi-thread.
Next: SDK quickstart, Generate text or Run through a gateway.