CODE EXAMPLES

Copy, paste, run — three real integrations

Complete, runnable scripts you can adapt in minutes. Every snippet uses model: "auto", so each request is routed to the best-fit model and you pay that model's normal rate — no routing fee.

0

One-time setup

All three scripts use the official OpenAI SDK pointed at NovaToken, with your key stored in an environment variable. Python 3.8+ works everywhere below.

# install the SDK once
pip install openai

# put your NovaToken key (starts with sk-) in an environment variable
# macOS / Linux:
export NOVATOKEN_API_KEY="sk-..."

# Windows (PowerShell):
setx NOVATOKEN_API_KEY "sk-..."
Prefer raw HTTP? Everything is plain OpenAI format — curl https://api.novatoken.dev/v1/chat/completions -H "Authorization: Bearer $NOVATOKEN_API_KEY" -d '{"model":"auto","messages":[{"role":"user","content":"Hi!"}]}' works too.
1

Streaming support bot

A terminal chat assistant with memory of the conversation. Replies stream in token-by-token like ChatGPT. Because short chat requests route to the smallest capable model, a full back-and-forth costs fractions of a cent.

# bot.py — pip install openai
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["NOVATOKEN_API_KEY"],
    base_url="https://api.novatoken.dev/v1",
)

SYSTEM = "You are the support assistant for Acme Widgets. Answer briefly and helpfully."
history = [{"role": "system", "content": SYSTEM}]

print("Support bot ready. Type 'quit' to exit.\n")

while True:
    user = input("You: ").strip()
    if not user or user.lower() == "quit":
        break
    history.append({"role": "user", "content": user})

    print("Bot: ", end="", flush=True)
    stream = client.chat.completions.create(
        model="auto",            # routed per-request, you pay the picked model's rate
        messages=history,
        stream=True,
    )
    reply = ""
    for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        print(delta, end="", flush=True)
        reply += delta
    print("\n")

    history.append({"role": "assistant", "content": reply})
Support bot ready. Type 'quit' to exit. You: where is my order? Bot: Could you share your order number? I'll check the status right away. You: it's AW-1042 Bot: Order AW-1042 shipped yesterday via DHL and arrives Thursday. Tracking is in your confirmation email.

Run it: python bot.py

2

Long-document summarizer

Splits a big text file into chunks, summarizes each, then merges the partials into one final summary. Long inputs are automatically routed to the long-context model — no manual model juggling.

# summarize.py — pip install openai
import os, sys
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["NOVATOKEN_API_KEY"],
    base_url="https://api.novatoken.dev/v1",
)

def summarize(text):
    r = client.chat.completions.create(
        model="auto",            # long inputs go to the long-context model automatically
        messages=[
            {"role": "system", "content": "Summarize the text in 3 concrete bullet points."},
            {"role": "user", "content": text},
        ],
    )
    return r.choices[0].message.content

def main():
    path = sys.argv[1] if len(sys.argv) > 1 else "doc.txt"
    text = open(path, encoding="utf-8").read()
    CHUNK = 12000
    pieces = [text[i:i+CHUNK] for i in range(0, len(text), CHUNK)]
    print(f"{len(text):,} chars in {len(pieces)} chunk(s)\n")

    partials = [summarize(p) for p in pieces]

    if len(partials) == 1:
        final = partials[0]
    else:
        final = summarize("Merge these summaries into one:\n\n" + "\n---\n".join(partials))

    print(final)

if __name__ == "__main__":
    main()
48,210 chars in 5 chunk(s) • Q3 revenue grew 18% quarter-over-quarter, driven by the EU launch… • Two production incidents (Aug 12, Sep 3) were both traced to the same… • Hiring plan adds 6 engineers in H1; the support backlog is the main…

Run it: python summarize.py report.txt

3

Batch translator

Translates a list of strings with four requests in flight at once — hundreds of lines processed in seconds for well under a cent each. Swap the target language or feed it a CSV; the pattern stays the same.

# translate.py — pip install openai
import os
from concurrent.futures import ThreadPoolExecutor
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["NOVATOKEN_API_KEY"],
    base_url="https://api.novatoken.dev/v1",
)

TEXTS = [
    "Shipping takes 3-5 business days.",
    "We offer a 30-day money-back guarantee.",
    "Contact support if your key stops working.",
]

def translate(text, target="Chinese"):
    r = client.chat.completions.create(
        model="auto",
        messages=[
            {"role": "system", "content": f"Translate to {target}. Output only the translation."},
            {"role": "user", "content": text},
        ],
        temperature=0.2,
    )
    return r.choices[0].message.content.strip()

with ThreadPoolExecutor(max_workers=4) as pool:
    results = list(pool.map(translate, TEXTS))

for src, dst in zip(TEXTS, results):
    print(src, "->", dst)
Shipping takes 3-5 business days. -> 配送需要 3-5 个工作日。 We offer a 30-day money-back guarantee. -> 我们提供 30 天退款保证。 Contact support if your key stops working. -> 如果您的密钥失效,请联系客服。

Run it: python translate.py

Cost sanity check: each of these snippets costs a fraction of a cent per run on auto. Your dashboard shows exactly which model answered every request, at that model's own rate.
→

Next steps

New here? The Quickstart gets you a key in 60 seconds, and the pricing table lists every model. Grab a key and run the first script in under two minutes.

Get API Key Read the Quickstart