Claude Go SDK — Quickstart

How to call the Claude API from Go in 2026. Covers the official SDK, streaming, prompt caching, tool use, and concurrency patterns for production Go services.

🔥 Launch tonight — Power Prompts PDF 50p (just 50p tonight)30 battle-tested Claude Code prompts · 8 pages · paste into CLAUDE.md · price reverts to £5

Anthropic ships an official Go SDK — github.com/anthropics/anthropic-sdk-go. It tracks the Python and TypeScript SDKs in feature parity (messages, streaming, batch, prompt caching, tool use, vision). This page is a working quickstart plus the Go-idiomatic patterns for production services.

Install

go get github.com/anthropics/anthropic-sdk-go@latest

Minimum-correct call

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/anthropics/anthropic-sdk-go"
    "github.com/anthropics/anthropic-sdk-go/option"
)

func main() {
    client := anthropic.NewClient(
        option.WithAPIKey("sk-ant-..."),
    )
    msg, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
        Model:     anthropic.F(anthropic.ModelClaudeSonnet4_6),
        MaxTokens: anthropic.F(int64(1024)),
        Messages: anthropic.F([]anthropic.MessageParam{
            anthropic.NewUserMessage(anthropic.NewTextBlock("Explain Go interfaces in 3 bullets.")),
        }),
    })
    if err != nil {
        log.Fatal(err)
    }
    for _, block := range msg.Content {
        if t, ok := block.AsAny().(anthropic.TextBlock); ok {
            fmt.Println(t.Text)
        }
    }
}

Streaming

stream := client.Messages.NewStreaming(ctx, anthropic.MessageNewParams{
    Model:     anthropic.F(anthropic.ModelClaudeSonnet4_6),
    MaxTokens: anthropic.F(int64(1024)),
    Messages:  anthropic.F([]anthropic.MessageParam{anthropic.NewUserMessage(anthropic.NewTextBlock("Write a haiku."))}),
})
for stream.Next() {
    event := stream.Current()
    if delta, ok := event.Delta.AsAny().(anthropic.RawContentBlockDeltaEventTextDelta); ok {
        fmt.Print(delta.Text)
    }
}
if err := stream.Err(); err != nil {
    log.Fatal(err)
}

Prompt caching

Cache a long system prompt by setting CacheControl on the prefix block:

system := []anthropic.TextBlockParam{{
    Type: anthropic.F(anthropic.TextBlockParamTypeText),
    Text: anthropic.F(longSystemPrompt),
    CacheControl: anthropic.F(anthropic.CacheControlEphemeralParam{
        Type: anthropic.F(anthropic.CacheControlEphemeralTypeEphemeral),
    }),
}}

Cached reads cost 10% of input price after the first request — see the caching deep-dive.

Concurrency patterns

Retries and rate limits

The Go SDK retries 429s and 5xx with exponential backoff out of the box. Configure with option.WithMaxRetries(int). For application-layer logic on 429, see 429 handling.

Tool use (function calling)

tools := []anthropic.ToolUnionParam{
    anthropic.F[anthropic.ToolUnionParam](anthropic.ToolParam{
        Name:        anthropic.F("get_weather"),
        Description: anthropic.F("Get current weather for a location."),
        InputSchema: anthropic.F(anthropic.ToolInputSchemaParam{
            Type: anthropic.F("object"),
            Properties: map[string]interface{}{
                "location": map[string]string{"type": "string"},
            },
        }),
    }),
}

The response will contain ToolUseBlock elements; execute them and feed results back as ToolResultBlock on the next call.

Cost in Go services

Wrap calls in middleware that records Usage.InputTokens, Usage.OutputTokens, Usage.CacheCreationInputTokens, and Usage.CacheReadInputTokens from every response. Multiply by the model's per-MTok rates to get cost-per-request. Estimate workload cost ahead of time with the Claude Cost Calculator.

For TypeScript and Python equivalents see TypeScript SDK and Python SDK.

Frequently asked questions

Is there an official Anthropic Go SDK for Claude?
Yes. github.com/anthropics/anthropic-sdk-go is the official SDK, maintained by Anthropic, with parity to the Python and TypeScript SDKs (messages, streaming, batch, caching, tools, vision).
Is the anthropic-sdk-go client safe for concurrent use?
Yes. A single *anthropic.Client can be shared across goroutines. Bound concurrency with a semaphore sized to your rate-limit tier to avoid TPM stampedes.
How do I cancel a Claude API request in Go?
Pass a context.Context to every SDK call. context.WithTimeout or context.WithCancel propagates through both streaming and non-streaming calls — when the context is cancelled, the underlying HTTP request is aborted and the SDK returns ctx.Err().
Does the Go SDK support prompt caching?
Yes. Set CacheControl on any TextBlockParam in your system or messages array. The Usage struct on responses reports CacheCreationInputTokens and CacheReadInputTokens so you can confirm cache hits.
Can I use the Go SDK with AWS Bedrock or Google Vertex?
Yes — the SDK supports both via option.WithBedrock() and option.WithVertex(). Authentication switches to AWS/GCP credentials; the messages API stays the same.

Free tools

Cost Calculator → Prompt-Pricing Recommender → Diff Summarizer → Skills Browser →

Related

Claude Opus 4.7 vs Sonnet 4.6 Pricing (2026 Comparison)How Much Does Claude Cost? (2026 API Pricing Guide)Claude Prompt Caching: 90% Cost Savings Explained (2026)Claude API Cost Calculator: Estimate Your Anthropic BillClaude vs GPT-4 Pricing: 2026 API Cost Comparison