AWS Bedrock
Amazon Bedrock exposes foundation models from
several vendors (Anthropic Claude, Meta Llama, Amazon Titan and Nova, and others)
behind one API. The llms/bedrock package talks to it through the AWS SDK.
Prerequisites
- AWS credentials with Bedrock access, resolved the standard AWS way (environment
variables such as
AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY/AWS_REGION, a shared profile, or an IAM role). - Access enabled for the specific model IDs you intend to call, in your region.
Setup
llm, err := bedrock.New(
bedrock.WithModel(bedrock.ModelAnthropicClaudeSonnet5),
)
If no model is set, the client defaults to Claude Haiku 4.5. Model IDs are available
as constants in the package (see models_list.go), or pass any Bedrock model ID
string. For non-Anthropic models whose provider cannot be inferred from the ID, set
it explicitly with bedrock.WithModelProvider.
Useful options:
bedrock.WithConverseAPI()— use the unified Converse API (required for tool use, structured output, and reasoning on supported models).bedrock.WithAutomaticCaching()— enable Anthropic prompt caching on supported Claude models.bedrock.WithClient(...)— supply a preconfigured*bedrockruntime.Client.
Example
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/vxcontrol/langchaingo/llms"
"github.com/vxcontrol/langchaingo/llms/bedrock"
)
func main() {
var (
modelID = flag.String("model", "amazon.titan-text-lite-v1", "Model ID to use")
provider = flag.String("provider", "", "Explicit provider (optional)")
prompt = flag.String("prompt", "Say hello in one word", "Prompt to send")
awsRegion = flag.String("region", "us-east-1", "AWS region")
verbose = flag.Bool("verbose", false, "Enable verbose output")
)
flag.Parse()
ctx := context.Background()
// Create Bedrock LLM options
opts := []bedrock.Option{
bedrock.WithModel(*modelID),
}
// Add explicit provider if specified
if *provider != "" {
opts = append(opts, bedrock.WithModelProvider(*provider))
if *verbose {
fmt.Printf("Using explicit provider: %s\n", *provider)
}
}
// Create LLM instance
llm, err := bedrock.New(opts...)
if err != nil {
log.Fatalf("Failed to create Bedrock LLM: %v", err)
}
if *verbose {
fmt.Printf("Model ID: %s\n", *modelID)
fmt.Printf("AWS Region: %s\n", *awsRegion)
fmt.Printf("Prompt: %s\n", *prompt)
fmt.Println("---")
}
// Test 1: Simple Call
fmt.Println("Testing Call method:")
response, err := llm.Call(ctx, *prompt)
if err != nil {
log.Printf("Error calling model: %v", err)
} else {
fmt.Printf("Response: %s\n", response)
}
// Test 2: GenerateContent with messages
fmt.Println("\nTesting GenerateContent method:")
messages := []llms.MessageContent{
{
Role: llms.ChatMessageTypeSystem,
Parts: []llms.ContentPart{
llms.TextPart("You are a helpful assistant."),
},
},
{
Role: llms.ChatMessageTypeHuman,
Parts: []llms.ContentPart{
llms.TextPart(*prompt),
},
},
}
resp, err := llm.GenerateContent(ctx, messages)
if err != nil {
log.Printf("Error generating content: %v", err)
} else {
if len(resp.Choices) > 0 && len(resp.Choices[0].Content) > 0 {
fmt.Printf("Response: %s\n", resp.Choices[0].Content)
}
}
}