Files
langchaingo/examples/openrouter-llm-example
Dmitry Ng 064f09155c chore(deps): bump Go toolchain to 1.26.5 and fix vulnerable dependencies
- Raised the `go` directive from 1.24.1 to 1.26.5 to satisfy pgx/v5 and ollama's minimum Go version requirements and to pick up 15 patched standard library CVEs.
- Updated `pgx/v5`, `otel`/`otel/sdk`/`otlptracehttp`, `x/net`, and the AWS `eventstream` protocol package to their latest requested versions.
- Remediated govulncheck-flagged CVEs in `grpc`, `x/text`, `x/crypto`, `antchfx/xpath`, AWS SDK `bedrock*`/`s3` services, `etcd/server`, and `mongo-driver` v1/v2.
- Fixed ollama's `MainGPU` API type change (`int` -> `*int`) in `llms/ollama/options.go` without breaking the public `WithRunnerMainGPU` signature.
2026-08-04 13:57:26 +03:00
..

OpenRouter LLM Example

This example demonstrates how to use OpenRouter with langchaingo. OpenRouter provides a unified API to access various LLM models through a single endpoint.

About OpenRouter

OpenRouter is an AI routing service that provides:

  • Access to multiple LLM providers (OpenAI, Anthropic, Google, Meta, etc.) through a single API
  • Automatic failover and load balancing
  • Usage tracking and analytics
  • Support for both free and premium models

Setup

  1. Get an OpenRouter API key from https://openrouter.ai/
  2. Set the environment variable:
    export OPENROUTER_API_KEY="your-api-key-here"
    

Usage

OpenRouter uses an OpenAI-compatible API, so you can use the OpenAI client with a custom base URL:

llm, err := openai.New(
    openai.WithModel("meta-llama/llama-3.2-3b-instruct:free"),
    openai.WithBaseURL("https://openrouter.ai/api/v1"),
    openai.WithToken(apiKey),
)

Available Models

OpenRouter provides access to many models including:

  • Free tier: meta-llama/llama-3.2-3b-instruct:free, google/gemma-2-9b-it:free
  • Premium: GPT-4, Claude, Gemini Pro, and many more
  • Check https://openrouter.ai/models for the full list

Running the Example

go run openrouter_llm_example.go

Features

  • Streaming responses supported
  • Compatible with OpenAI client
  • Access to multiple model providers
  • Automatic handling of provider-specific quirks