anthropic

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 4

README

Anthropic Plugin

This plugin provides Genkit support for Claude models through Anthropic's OpenAI-compatible Chat Completions endpoint.

Anthropic positions that endpoint as a compatibility layer for testing and comparing Claude rather than a long-term integration surface: it does not return thinking content, ignores response_format, and hoists system messages to the start of the conversation. The full support matrix is at https://platform.claude.com/docs/en/cli-sdks-libraries/libraries/openai-sdk. For the native Messages API surface (thinking output, prompt caching, citations, structured outputs), use the go/plugins/anthropic plugin instead; this one is for code that must speak the OpenAI shape.

Setup

Set an Anthropic API key:

export ANTHROPIC_API_KEY=<your-api-key>

The plugin's APIKey field overrides the environment. The plugin uses https://api.anthropic.com/v1 by default; set ANTHROPIC_BASE_URL or pass option.WithBaseURL through the plugin's Opts to use another compatible endpoint.

import (
    "context"

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

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

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

Models

A catalog of dated Claude releases is registered at Init, and the catalog is not a ceiling: any Claude model ID resolves on demand, and the plugin lists what Anthropic's native models API reports (paged, authenticated with x-api-key, since listing is not part of the compatible surface). Use the Models field to describe or correct any model, most often one released after this plugin:

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

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

Config

Models take a typed anthropic.ChatConfig: the fields the compatible endpoint honors, and none of the OpenAI fields it documents as ignored (the penalties, logprobs, seed). temperature runs 0 to 1, where the endpoint caps it. anthropic.ModelRef carries the config with the model ID:

response, err := genkit.Generate(ctx, g,
    ai.WithModel(anthropic.ModelRef("claude-sonnet-4-5-20250929", &anthropic.ChatConfig{
        MaxOutputTokens: 2048,
        Thinking:        &anthropic.ThinkingConfig{Type: "enabled", BudgetTokens: 2000},
    })),
    ai.WithPrompt("Work through this carefully."),
)

thinking spends a reasoning budget, but the compatible endpoint does not return the thinking content itself; only the native API does. On Claude 5 models thinking is adaptive and on by default, which makes the manual control a legacy mode.

Every config also carries the settings Genkit owns: version pins the exact model version a request is served by, apiKey (settable only from Go code) serves one request with a different credential, and extra forwards request body fields the config does not declare, keyed by Anthropic's wire names.

Live tests

Live tests are skipped unless ANTHROPIC_API_KEY is set:

go test -v ./plugins/compat_oai/anthropic

Documentation

Overview

Package anthropic provides a Genkit plugin for Claude models through Anthropic's OpenAI-compatible endpoint. Anthropic positions that endpoint for testing and comparison; the plugins/anthropic package speaks the native Anthropic API and is the primary way to use Claude models with Genkit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModelRef added in v1.12.0

func ModelRef(id string, config *ChatConfig) 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-3-5-haiku-20241022", &anthropic.ChatConfig{
	MaxOutputTokens: 1024,
}))

id is the model ID, with or without the provider prefix.

Types

type Anthropic

type Anthropic struct {
	// APIKey is the key requests are authenticated with. When empty, the
	// ANTHROPIC_API_KEY environment variable is used. A key set here or in
	// the environment authenticates both surfaces the plugin speaks to: the
	// chat endpoint as the bearer token and the native models list as
	// x-api-key.
	APIKey string

	// Opts are additional options for the underlying client, such as
	// [option.WithBaseURL] for a different endpoint; they are applied after
	// what the plugin composes, so they win on overlap. The credential
	// belongs in APIKey or the environment rather than here: a key inside
	// Opts is opaque to the plugin, so model listing cannot be authenticated
	// with it.
	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: 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 for a model released after this version of
	// the plugin.
	//
	//	&anthropic.Anthropic{Models: map[string]ai.ModelOptions{
	//		"claude-sonnet-4-5-20250929": {Supports: &ai.ModelSupports{Multiturn: true, Tools: 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
	// versions. Entries apply to the models Init registers as well as the
	// ones [Anthropic.ListActions] advertises and [Anthropic.ResolveAction] builds,
	// which is the way to describe a curated model differently: Init has
	// already registered those and nothing can re-register them.
	Models map[string]ai.ModelOptions
	// contains filtered or unexported fields
}

func (*Anthropic) DefineModel deprecated

func (a *Anthropic) DefineModel(id string, opts ai.ModelOptions) ai.Model

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

Deprecated: describe the model through Anthropic.Models instead. This method builds the model and registers nothing, 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

func (*Anthropic) ListActions added in v1.12.0

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

ListActions lists the Claude models the configured endpoint exposes, described by the plugin's config schema and capabilities.

func (*Anthropic) Model deprecated

func (a *Anthropic) 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-3-5-haiku-20241022") or, to carry config with it, ModelRef. Use genkit.LookupModel when the action itself is what you need.

func (*Anthropic) Name

func (a *Anthropic) Name() string

Name implements genkit.Plugin.

func (*Anthropic) ResolveAction added in v1.12.0

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

ResolveAction dynamically builds a Claude model, described by the plugin's config schema and capabilities.

type ChatConfig added in v1.12.0

type ChatConfig struct {
	compat_oai.RequestConfig

	// Temperature controls the degree of randomness, from 0 to 1; the
	// endpoint caps greater values at 1, so the schema stops where the
	// behavior does.
	Temperature *float64 `` /* 220-byte string literal not displayed */
	// MaxOutputTokens is the maximum number of tokens to generate, sent as
	// the API's max_tokens.
	MaxOutputTokens int `` /* 148-byte string literal not displayed */
	// TopP is the nucleus sampling threshold. The endpoint documents no
	// range for it, so the schema declares none.
	TopP *float64 `` /* 144-byte string literal not displayed */
	// StopSequences stop generation when produced by the model; whitespace
	// stop sequences are not supported by the endpoint.
	StopSequences []string `` /* 165-byte string literal not displayed */
	// Thinking controls Claude's extended thinking.
	Thinking *ThinkingConfig `` /* 154-byte string literal not displayed */
}

ChatConfig is the per-request config for Claude models served through Anthropic's OpenAI-compatible endpoint. It carries the fields that endpoint honors; OpenAI fields Anthropic documents as ignored (penalties, log probabilities, seed, response_format) are deliberately absent. See https://platform.claude.com/docs/en/api/openai-sdk.

func (ChatConfig) ApplyToChatCompletion added in v1.12.0

func (c ChatConfig) ApplyToChatCompletion(params *openai.ChatCompletionNewParams)

ApplyToChatCompletion implements compat_oai.ChatConfig: the endpoint's generation fields land on their chat completion counterparts and thinking rides as the endpoint's thinking extra field.

type ThinkingConfig added in v1.12.0

type ThinkingConfig struct {
	// Type turns thinking "enabled" or "disabled". It is not an enum in the
	// schema: the endpoint documents no closed set, and the native API's set
	// has grown (Claude 5 added adaptive thinking), so a list here would
	// reject a value Anthropic accepts.
	Type string `json:"type,omitempty" jsonschema_description:"Turns thinking enabled or disabled."`
	// BudgetTokens is the maximum number of tokens Claude may think with,
	// sent as the API's budget_tokens. Anthropic rejects budgets under 1,024
	// tokens, and the budget must stay below the request's max_tokens.
	BudgetTokens int `` /* 208-byte string literal not displayed */
}

ThinkingConfig configures Claude's extended thinking through the OpenAI-compatible endpoint. The endpoint does not return the thinking content; the plugins/anthropic package does.

Jump to

Keyboard shortcuts

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