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.
Make a request
Section titled “Make a request”This example connects to OpenAI and asks a model a question.
import std/osimport nimgentimport 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.textSet your API key and run it:
export OPENAI_API_KEY=...nim c -r hello.nimopenAI(...) creates a provider, and .model(...) selects a provider-specific model ID. The provider checks whether that model is available when you make the request.
Choose a provider
Section titled “Choose a provider”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/osimport nimgentimport 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 = falselet model = if useFallback: fallback else: fastecho generateText(model, prompt = "Summarize this file.").textConfigure a request
Section titled “Configure a request”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.
Troubleshooting
Section titled “Troubleshooting”- 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.
Next steps
Section titled “Next steps”Open a provider setup page above, then continue with Streaming or Tools and agents.