The Master Key for LLMs: My Own OpenRouter Go SDK
Picture this: you’re standing in front of a massive building with hundreds of doors. Originally, behind each door hid a different large language model (LLM) – GPT-5.5, Claude 4.7 Opus, Gemini 3.1 Pro, or some obscure new open-source model. But today, the building has expanded. Now, those doors also open to Image Generation, Voice Agents and Text-to-Speech, Video Generation, and models capable of processing Multi-modal inputs like PDFs, images, and audio. Normally, you’d need a separate key for every damn door (read: a separate API integration for each modality and provider).
OpenRouter is the master key. One API, one endpoint, and suddenly all of these models and capabilities are at your fingertips. The concept is brilliant. But when I started using OpenRouter heavily in my Go projects (like my ai CLI tool), I ran into a problem.
Why build my own SDK in the first place?
Don’t get me wrong – you can talk to OpenRouter through the official OpenAI SDK, since the API is largely kept compatible. But it always felt like a compromise. OpenRouter-specific features were missing, and a lot of existing libraries dragged a massive rat’s tail of dependencies behind them.
I wanted something different. Something real. A tool that feels like idiomatic Go. So I sat down and built openrouter-go.
My principles were strict from day one:
- Zero Dependencies: Only the Go standard library (Go 1.26). No bloat, no nasty surprises during security scans.
- Type Safety: Instead of wild JSON juggling, I wanted clean structs and an elegant functional options pattern (
WithModel(...),WithTemperature(...)). - Pure Go Feel: Asynchronous streaming via channels, clean handling of
context.Context, and full thread safety out of the box.
The Key Features
The SDK now covers 100% of the OpenRouter API (yes, including the brand-new Responses API), but there are a few things I’m particularly proud of:
- Streaming (Server-Sent Events): Nothing is more annoying than blocking APIs during text generation. The SDK parses SSE natively and delivers the generated tokens via channel straight into your terminal or UI.
- Tool Calling & Structured Outputs: Want strict JSON schema enforcement? Check. Want to seamlessly convert MCP (Model Context Protocol) tools and use them in agents? Also check.
- Zero Data Retention (ZDR): As an IT Operator, I’m paranoid about sensitive company data. With the SDK, you can explicitly enforce ZDR on a per-request basis. Is the provider behind the model storing your data for training? Then OpenRouter blocks the request. Privacy by design.
- Provider Routing & Fallbacks: If a model provider goes down, you can instruct the SDK to route to the next provider directly and without delay, or set hard price limits (
WithMaxPrice).
Care for a quick example?
Because code says more than a thousand words – here’s how simple a request with full context and privacy guardrails looks:
package main
import (
"context"
"fmt"
"github.com/hra42/openrouter-go"
)
func main() {
// Initialize the client (API key can also come from the environment)
client := openrouter.NewClient(
openrouter.WithAPIKey("sk-or-v1-..."),
)
// Build the request
response, err := client.ChatComplete(context.Background(),
openrouter.WithModel("anthropic/claude-3-opus"),
openrouter.WithMessages([]openrouter.Message{
{Role: "user", Content: "Explain Kubernetes in one sentence."},
}),
openrouter.WithZDR(true), // <- Enforce Zero Data Retention!
)
if err != nil {
panic(err)
}
fmt.Println(response.Choices[0].Message.Content)
}
Final Thoughts
I’m “The non-classical developer.” I don’t spend all day writing microservices on a 50-person software engineering team. I’m primarily an operator, a sysadmin, a DevOps person. I build tools first and foremost to ease my own pain in the day-to-day and to automate workflows.
But that’s exactly what’s beautiful about the open-source world: when you build a tool that makes your own life easier, there’s a good chance it’ll help someone else too. The openrouter-go SDK is exactly that – a pragmatic, robust, and secure piece of code that bridges the gap between the highly complex LLM landscape and our daily work.
If you’re building with Go and AI, give it a try. You’ll find detailed docs and plenty of recipes at openrouter-go.hra42.lol, and the code of course on GitHub. Issues and PRs are always welcome.