The short version: NanoGPT's API is a drop-in replacement for OpenAI's. Swap the base URL and API key, and your existing code works unchanged. I migrated a SillyTavern setup and a few Python scripts in about 10 minutes total.
The OpenAI-Compatible API
NanoGPT's API is OpenAI-compatible. Same library, same methods, same everything. You just swap the base URL and API key. That's the whole migration. No rewrites, no new SDK to learn.
If you've ever written a script against the OpenAI API, you already know how to use NanoGPT. The only difference is the endpoint URL and the key. Everything else works exactly the same.
Step 1: Install the OpenAI Library
pip install openaiStep 2: Basic Python Example
Swap in your API key from the NanoGPT dashboard and run it. That's it.
from openai import OpenAI
client = OpenAI(
api_key="your-nanogpt-api-key",
base_url="https://nano-gpt.com/api/v1"
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Explain quantum computing in 3 sentences"}
]
)
print(response.choices[0].message.content)Step 3: Switch Models
Change the model string to switch models. Nothing else in your code changes. I use this constantly when I want Claude for one task and GPT-4o for another.
# GPT-4o for complex tasks
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a Python quicksort"}]
)
# Claude 3.5 Sonnet for writing
response = client.chat.completions.create(
model="claude-3-5-sonnet",
messages=[{"role": "user", "content": "Write a short story about a robot"}]
)
# DeepSeek V3 for math
response = client.chat.completions.create(
model="deepseek-v3",
messages=[{"role": "user", "content": "Solve: integral of x^2 * e^x dx"}]
)Step 4: Streaming Responses
For long responses, streaming lets you see tokens arrive in real time instead of waiting for the full response. Way better UX for anything user-facing.
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Write a 500 word essay about climate change"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Step 5: Using with SillyTavern
SillyTavern uses the OpenAI-compatible API format. In the API settings:
- Set API type to OpenAI / Completions
- Set the endpoint URL to NanoGPT's API base URL
- Paste your NanoGPT API key
- Select a model from the dropdown
I tested this with SillyTavern and it worked right away, no config weirdness. One thing to note: the API documentation from NanoGPT is pretty sparse. You'll mostly be looking at OpenAI's docs and it maps 1:1, but don't expect a detailed NanoGPT-specific guide.
Step 6: Using with curl
You can also hit the API directly with curl. Handy for quick tests without writing a script.
curl https://nano-gpt.com/api/v1/chat/completions \
-H "Authorization: Bearer your-n...-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Hello!"}]
}'Error Handling
Worth adding this from the start. I learned the hard way that rate limit errors can crash your script at 3am if you're running a batch job.
from openai import OpenAI, APIError, RateLimitError
client = OpenAI(
api_key="your-nanogpt-api-key",
base_url="https://nano-gpt.com/api/v1"
)
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
except RateLimitError:
print("Rate limited. Wait a moment and retry.")
except APIError as e:
print(f"API error: {e}")API Key Security
Don't hardcode your API key in scripts you share or push to Git. Use environment variables: set NANOGPT_API_KEY and read it with os.environ.get("NANOGPT_API_KEY"). I've seen people accidentally commit keys to public repos. It's not fun.