genkit_anthropic 0.3.1 copy "genkit_anthropic: ^0.3.1" to clipboard
genkit_anthropic: ^0.3.1 copied to clipboard

Anthropic plugin for Genkit Dart, providing Claude model integration with tool calling, multimodal inputs, and streaming support.

Pub

Anthropic plugin for Genkit Dart.

Usage #

Initialization #

import 'dart:io';
import 'package:genkit/genkit.dart';
import 'package:genkit_anthropic/genkit_anthropic.dart';

void main() async {
  // Initialize Genkit with the Anthropic plugin
  // Make sure ANTHROPIC_API_KEY is allowed in your environment
  final ai = Genkit(
    plugins: [anthropic(apiKey: Platform.environment['ANTHROPIC_API_KEY']!)],
  );
}

Basic Generation #

final response = await ai.generate(
  model: anthropic.model('claude-sonnet-4-5'),
  prompt: 'Tell me a joke about a developer.',
);
print(response.text);

Streaming #

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 #

import 'package:schemantic/schemantic.dart';

part 'main.g.dart';

@Schema()
abstract class $CalculatorInput {
  int get a;
  int get b;
}

// ... inside main ...

ai.defineTool(
  name: 'calculator',
  description: 'Multiplies two numbers',
  inputSchema: CalculatorInput.$schema,
  outputSchema: .integer(),
  fn: (input, context) async => .response(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 #

final response = await ai.generate(
  model: anthropic.model('claude-sonnet-5'),
  prompt: 'Solve this 24 game: 2, 3, 10, 10',
  config: AnthropicOptions(
    // Uses the model's compatible default thinking mode.
    thinking: ThinkingConfig(),
    outputConfig: AnthropicOutputConfig(effort: 'high'),
  ),
);

// The thinking content is available in the message parts
print(response.message?.content);

An omitted thinking type resolves to the model's own default: Claude 4.6 and newer default to adaptive, while Claude 4.5 models default to enabled, which uses a manual token budget. You can also select a mode explicitly with ThinkingConfig(type: 'enabled', budgetTokens: 2048). For model names outside the curated catalog, set thinking.type explicitly so the plugin does not guess an incompatible mode.

Structured Output #

@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 = response.output; // Typed Person object
print('Name: ${person.name}, Age: ${person.age}');
1
likes
130
points
5.53k
downloads

Documentation

API reference

Publisher

verified publishergenkit.dev

Weekly Downloads

Anthropic plugin for Genkit Dart, providing Claude model integration with tool calling, multimodal inputs, and streaming support.

Repository (GitHub)
View/report issues
Contributing

License

Apache-2.0 (license)

Dependencies

anthropic_sdk_dart, genkit, http, logging, meta, schemantic

More

Packages that depend on genkit_anthropic