Skip to content

Anthropic

Connect to Anthropic with an API key, choose a Claude model, and use it with the usual nimgent generation APIs.

import std/os
import nimgent
import nimgent/providers/anthropic
let model = anthropic(getEnv("ANTHROPIC_API_KEY")).model("claude-sonnet-4-6")
echo generateText(model, prompt = "Say hello in one sentence.").text

Set ANTHROPIC_API_KEY before you run the program.

Use AnthropicOptions when you want to enable Anthropic thinking with a budget.

import std/options
let response = generateText(
model,
prompt = "Solve this carefully.",
providerOptions = ProviderOptions(
anthropic: AnthropicOptions(
thinking: some(EnabledThinking),
budgetTokens: some(1024)))
)

EnabledThinking requires budgetTokens, and the minimum budget is 1024. Use AdaptiveThinking when you want Anthropic to choose the thinking budget.

See Settings for portable controls, or Structured output for typed results.