Skip to content

Providers

Choose a provider, bind one of its model IDs, and use that model with nimgent’s generation APIs. Switching providers changes the setup, not how you call generateText, stream output, use tools, or create structured results.

This example connects to OpenAI and asks a model a question.

hello.nim
import std/os
import nimgent
import nimgent/providers/openai
let model = openAI(getEnv("OPENAI_API_KEY")).model("gpt-4.1-mini")
let response = generateText(model, prompt = "Say hello in one sentence.")
echo response.text

Set your API key and run it:

Terminal window
export OPENAI_API_KEY=...
nim c -r hello.nim

openAI(...) creates a provider, and .model(...) selects a provider-specific model ID. The provider checks whether that model is available when you make the request.

Each provider has its own setup page and API key environment variable.

Provider Constructor Setup
OpenAI openAI(apiKey) OpenAI
Anthropic anthropic(apiKey) Anthropic
Google Gemini google(apiKey) Google Gemini
OpenRouter openRouter(apiKey) OpenRouter
Hyper hyper(apiKey) Hyper
Mistral mistral(apiKey) Mistral
OpenCode and OpenCode Zen openCode(apiKey) OpenCode
Another API Your own Provider subtype Custom provider

You can keep more than one model ready and choose one at runtime:

import std/os
import nimgent
import nimgent/providers/[anthropic, openai]
let fast = openAI(getEnv("OPENAI_API_KEY")).model("gpt-4.1-mini")
let fallback = anthropic(getEnv("ANTHROPIC_API_KEY")).model("claude-sonnet-4-6")
let useFallback = false
let model = if useFallback: fallback else: fast
echo generateText(model, prompt = "Summarize this file.").text

Use Settings for generation controls that travel with a model switch, such as temperature and stop sequences. It also explains provider-specific settings for features that only one provider offers.

  • Authentication fails: Confirm the environment variable matches the provider you constructed and is available to your program.
  • The model is not found: Copy the exact model ID from the selected provider’s catalog.
  • A feature is rejected: Support can vary by model. Handle the provider error, then select a compatible model or adjust the request.

Open a provider setup page above, then continue with Streaming or Tools and agents.