Anthropic Plugin
The genkit_anthropic package provides a unified interface to connect with Anthropic’s Claude models through the Anthropic API.
Installation
Section titled “Installation”dart pub add genkit_anthropicConfiguration
Section titled “Configuration”To use this plugin, import it and specify it when you initialize Genkit:
import 'dart:io';import 'package:genkit/genkit.dart';
import 'package:genkit_anthropic/genkit_anthropic.dart';
void main() async { // Initialize Genkit with the Anthropic plugin final ai = Genkit( plugins: [anthropic(apiKey: Platform.environment['ANTHROPIC_API_KEY']!)], );}The plugin requires an Anthropic API Key, which you can get from the Anthropic Console.
Language Models
Section titled “Language Models”You can reference Claude models, e.g. claude-sonnet-4-5 or 'claude-sonnet-4-6'.
import 'dart:io';import 'package:genkit/genkit.dart';import 'package:genkit_anthropic/genkit_anthropic.dart';
void main() async { final ai = Genkit( plugins: [anthropic(apiKey: Platform.environment['ANTHROPIC_API_KEY']!)], );
final response = await ai.generate( model: anthropic.model('claude-sonnet-4-5'), prompt: 'Tell me a joke about a developer.', ); print(response.text);}Streaming
Section titled “Streaming”The plugin supports streaming responses natively.
final stream = ai.generateStream( model: anthropic.model('claude-sonnet-4-5'), prompt: 'Count to 5',);
await for (final chunk in stream) { print(chunk.text);}
final response = await stream.onResult;print('Full response: \${response.text}');Tool Calling
Section titled “Tool Calling”Claude models support function calling and tool use.
import 'package:schemantic/schemantic.dart';
// part 'main.g.dart'; // generated by build_runner
@Schema()abstract class $CalculatorInput { int get a; int get b;}
// ... inside main ...
ai.defineTool( name: 'calculator', description: 'Multiplies two numbers', inputSchema: CalculatorInput.$schema, outputSchema: intSchema(), fn: (input, context) async => input.a * input.b,);
final response = await ai.generate( model: anthropic.model('claude-sonnet-4-5'), prompt: 'What is 123 * 456?', toolNames: ['calculator'],);
print(response.text);Thinking (Claude 3.7+)
Section titled “Thinking (Claude 3.7+)”Claude 3.7 models support explicit thinking to expose the reasoning process before returning the final response.
final response = await ai.generate( model: anthropic.model('claude-sonnet-4-5'), prompt: 'Solve this 24 game: 2, 3, 10, 10', config: AnthropicOptions(thinking: ThinkingConfig(budgetTokens: 2048)),);
// The thinking content is available in the message partsprint(response.message?.content);print(response.text);Structured Output
Section titled “Structured Output”import 'package:schemantic/schemantic.dart';
// part 'main.g.dart'; // generated by build_runner
@Schema()abstract class $Person { String get name; int get age;}
// ... inside main ...
final response = await ai.generate( model: anthropic.model('claude-sonnet-4-5'), prompt: 'Generate a person named John Doe, age 30', outputSchema: Person.$schema,);
final person = Person.fromJson(response.output!);print('Name: \${person.name}, Age: \${person.age}');