Skip to main content

Anthropic

Anthropic's Claude models support long context, tool use, and extended ("thinking") reasoning through the llms/anthropic package.

Setup

Set your API key in the ANTHROPIC_API_KEY environment variable, or pass it explicitly:

llm, err := anthropic.New(
anthropic.WithModel("claude-sonnet-4-5-20250929"),
anthropic.WithToken("your-api-key"), // or ANTHROPIC_API_KEY
)

Extended thinking is controlled per call with the provider-neutral reasoning options (llms.WithReasoning, llms.WithAdaptiveReasoning, llms.WithReasoningDisabled); the adapter resolves the correct wire form (budget vs. adaptive) from the model.

For a full walkthrough, see the Anthropic quickstart.

Example

package main

import (
"context"
"fmt"
"log"

"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/anthropic"
"github.com/vxcontrol/langchaingo/llms/streaming"
)

func main() {
llm, err := anthropic.New(
anthropic.WithModel("claude-3-5-sonnet-20240620"),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completion, err := llms.GenerateFromSinglePrompt(ctx, llm, "Hi claude, write a poem about golang powered AI systems",
llms.WithTemperature(0.8),
llms.WithStreamingFunc(func(_ context.Context, chunk streaming.Chunk) error {
fmt.Println(chunk.String())
return nil
}),
)
if err != nil {
log.Fatal(err)
}

_ = completion
}