anthropic

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 3

README

Anthropic Plugin

This plugin provides Genkit support for Claude models through Anthropic's native Messages API, using the official Go SDK, github.com/anthropics/anthropic-sdk-go.

Unlike the OpenAI-compatible compat_oai/anthropic plugin, this one speaks the Messages API itself: thinking content comes back as Genkit reasoning parts with their signatures preserved across turns, structured output uses Anthropic's native support, and Anthropic's server-side tools are reachable.

Setup

Set an Anthropic API key:

export ANTHROPIC_API_KEY=<your-api-key>

ANTHROPIC_BASE_URL overrides the endpoint; both settings can also be set as plugin struct fields.

import (
    "context"

    "github.com/firebase/genkit/go/ai"
    "github.com/firebase/genkit/go/genkit"
    "github.com/firebase/genkit/go/plugins/anthropic"
)

ctx := context.Background()
g := genkit.Init(
    ctx,
    genkit.WithPlugins(&anthropic.Anthropic{}),
    genkit.WithDefaultModel("anthropic/claude-sonnet-4-5"),
)

response, err := genkit.Generate(ctx, g, ai.WithPrompt("Explain constitutional AI."))

Claude's thinking output is returned as Genkit reasoning parts, available through response.Reasoning(), and each block's signature rides along so multi-turn conversations replay their thinking intact.

Models

Init registers nothing: every Claude model resolves on first use, and the plugin lists Anthropic's current catalog through the models API, cached for an hour. Curated entries describe the current families (claude-fable-5, claude-opus-5, claude-sonnet-5, the Claude 4 line, claude-haiku-4-5), dated snapshots such as claude-sonnet-4-5-20250929 resolve to the same descriptions, and the Models field describes or corrects any model, most often one released after this plugin:

plugin := &anthropic.Anthropic{Models: map[string]ai.ModelOptions{
    "claude-opus-6": {Label: "Claude Opus 6"},
}}

The current model list is at https://platform.claude.com/docs/en/about-claude/models/overview.

Config

Models take the SDK's own request type, anthropic.MessageNewParams, with Anthropic's wire names. This package and the SDK are both named anthropic, so alias one of them:

import (
    sdk "github.com/anthropics/anthropic-sdk-go"

    "github.com/firebase/genkit/go/ai"
    "github.com/firebase/genkit/go/genkit"
    "github.com/firebase/genkit/go/plugins/anthropic"
)

response, err := genkit.Generate(ctx, g,
    ai.WithModel(anthropic.ModelRef("claude-opus-4-5", &sdk.MessageNewParams{
        MaxTokens: 1024,
    })),
    ai.WithPrompt("Answer concisely."),
)

The fields Genkit builds from the request are not config: a config carrying messages, system, model, an output format, or a custom function tool is rejected with an error naming the Genkit option to use instead (ai.WithMessages, ai.WithSystem, ai.WithModel, ai.WithOutputType, ai.WithTools). The config-level tools field stays available for Anthropic's server-side tools, such as web search and code execution.

The advertised schema is reflected from the anthropic-sdk-go version your build links, so a field Anthropic ships tomorrow becomes usable, and validated, by bumping the SDK in your own go.mod.

The API reference is at https://platform.claude.com/docs/en/api/overview.

Live tests

Live tests are skipped unless ANTHROPIC_API_KEY is set:

go test -v ./plugins/anthropic

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDefinedModel deprecated

func IsDefinedModel(g *genkit.Genkit, id string) bool

IsDefinedModel reports whether a model is already registered. The lookup deliberately does not resolve dynamically: a resolving lookup would ask the plugin to resolve the very model the caller is checking for, registering it and answering true for any ID the Anthropic API can serve.

Deprecated: this existed to guard a registration call that could panic on a duplicate. Capabilities now come from Anthropic.Models, which nothing has to register and which no ordering can defeat, leaving this a question about registry state that applications do not need to ask.

func Model deprecated

func Model(g *genkit.Genkit, id string) ai.Model

Model returns a previously registered model.

Deprecated: Generation resolves a model from its name, so looking one up first is rarely necessary: pass ai.WithModelName("anthropic/claude-opus-4-5") or, to carry config with it, ModelRef. Use genkit.LookupModel when the action itself is what you need.

func ModelRef added in v1.12.0

func ModelRef(id string, config *anthropic.MessageNewParams) ai.ModelRef

ModelRef names a Claude model and carries the config to generate with, so the config is typed at the call site instead of an any the model checks at runtime. A nil config leaves the request's config unset.

ai.WithModel(anthropic.ModelRef("claude-opus-4-5", &sdk.MessageNewParams{
	MaxTokens: 1024,
}))

id is the model ID, with or without the provider prefix: "claude-opus-4-5" and "anthropic/claude-opus-4-5" name the same model, as they do everywhere else in this package.

This package and the Anthropic SDK are both named anthropic, so one of them needs an import alias; the example above aliases the SDK to sdk.

Types

type Anthropic

type Anthropic struct {
	// APIKey is the key requests are authenticated with. When empty, the
	// ANTHROPIC_API_KEY environment variable is used, and [Anthropic.Init]
	// panics unless authentication arrives another way: an auth token in
	// ANTHROPIC_AUTH_TOKEN, or a non-empty Opts.
	APIKey string

	// BaseURL overrides the API endpoint requests are sent to. When empty, the
	// ANTHROPIC_BASE_URL environment variable is used, and failing that the
	// SDK's own default.
	BaseURL string

	// Opts are anthropic-sdk-go request options applied to every request the
	// client sends. They open the SDK's full client configuration to the
	// plugin: [option.WithMiddleware], [option.WithMaxRetries],
	// [option.WithRequestTimeout], extra headers, and the SDK's bedrock and
	// vertex packages, whose routing helpers are request options too.
	//
	// They are applied after the options derived from APIKey and BaseURL, and
	// the SDK applies options in order, so an option here wins over those
	// fields when it sets the same setting. Distinct settings do not displace
	// each other: the API key rides an X-Api-Key header and an auth token an
	// authorization header, so a configured key (here, in APIKey, or in
	// ANTHROPIC_API_KEY, which the SDK reads on its own) is still sent
	// alongside whatever Opts configure. A key-less setup such as Bedrock or
	// Vertex routing therefore needs the key unset everywhere, or an
	// [option.WithHeaderDel] for X-Api-Key here, which applies after the key
	// options and so strips the header.
	//
	// Options are opaque, so a non-empty Opts is trusted to carry
	// authentication when no API key is configured.
	Opts []option.RequestOption

	// Models overrides what the plugin knows about a Claude model, keyed by
	// model ID, bare or provider-prefixed. Every Claude model already works
	// without an entry here: known IDs carry curated capabilities and the rest
	// take the Claude defaults. Supply an entry only to correct or extend what
	// the plugin resolves, most often to describe a model released after this
	// version of the plugin.
	//
	//	&anthropic.Anthropic{Models: map[string]ai.ModelOptions{
	//		"claude-opus-4-5": {Supports: &ai.ModelSupports{Tools: true, Multiturn: true}},
	//	}}
	//
	// Fields left at their zero value keep what the plugin resolves, so an
	// entry can pin one capability without restating the label or the config
	// schema. Entries apply everywhere a model is described: the actions
	// [Anthropic.ListActions] advertises and the ones
	// [Anthropic.ResolveAction] builds to serve a request.
	Models map[string]ai.ModelOptions
	// contains filtered or unexported fields
}

Anthropic is a Genkit plugin for interacting with the Anthropic API.

func (*Anthropic) DefineModel deprecated

func (a *Anthropic) DefineModel(g *genkit.Genkit, id string, opts *ai.ModelOptions) (ai.Model, error)

DefineModel builds a Claude model and returns it, without registering it with g.

Deprecated: describe the model through Anthropic.Models instead. This method builds the model and ignores g, so the result carries only the model's name: generation resolves a model from that name and serves the request with the capabilities the plugin resolves, not the ones passed here. An entry in Models reaches both paths.

func (*Anthropic) Init

func (a *Anthropic) Init(ctx context.Context) []api.Action

Init prepares the plugin to serve models and registers none: every Claude model arrives through Anthropic.ResolveAction on first use.

It panics when no authentication is configured (an API key in APIKey or ANTHROPIC_API_KEY, an auth token in ANTHROPIC_AUTH_TOKEN, or a non-empty Opts, which is trusted to carry its own) and when called twice, both being setup mistakes rather than conditions an application can recover from.

func (*Anthropic) ListActions

func (a *Anthropic) ListActions(ctx context.Context) []api.ActionDesc

ListActions describes every model the API currently advertises. A discovery failure is reported as an empty catalog rather than an error, since the interface has nowhere to return one.

func (*Anthropic) Name

func (a *Anthropic) Name() string

Name returns the plugin's name, which is also the provider prefix on the action name of every model it serves.

func (*Anthropic) ResolveAction

func (a *Anthropic) ResolveAction(atype api.ActionType, id string) api.Action

ResolveAction builds the model named by a request. Models are the only action type this plugin serves.

The ID is passed through to the API untouched. Anthropic resolves an alias like claude-opus-4-5 to its current dated release itself, so there is nothing to look up and nothing to validate here: the API is the authority on whether a model exists, and it answers when the request is made. Building an action is local work either way.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL