genkit_anthropic 0.0.1-dev.9 copy "genkit_anthropic: ^0.0.1-dev.9" to clipboard
genkit_anthropic: ^0.0.1-dev.9 copied to clipboard

Anthropic plugin for Genkit Dart. Provides full support for the Claude family of models with advanced features like tool calling, multimodal inputs, and Genkit's unified generate API.

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']!)],
  );
}

Claude on Vertex AI #

You can route Anthropic Claude requests through Vertex AI by providing a vertex config instead of an Anthropic API key.

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

void main() async {
  final ai = Genkit(
    plugins: [
      anthropic(
        vertex: AnthropicVertexConfig.adc(
          projectId: 'my-gcp-project',
          location: 'global',
        ),
      ),
    ],
  );

  final response = await ai.generate(
    model: anthropic.model('claude-sonnet-4-5'),
    prompt: 'Say hello from Vertex AI',
  );

  print(response.text);
}

Notes:

  • AnthropicVertexConfig.adc(...) uses Google Application Default Credentials.
  • ADC supports GOOGLE_APPLICATION_CREDENTIALS, local gcloud ADC login, and metadata server credentials (for Workload Identity / attached service accounts).
  • For explicit service account keys, use AnthropicVertexConfig.serviceAccount(...).
  • ADC and service-account helper constructors are available on Dart IO runtimes.
  • projectId is optional. If omitted, the plugin tries service-account project_id (when available) and then GOOGLE_CLOUD_PROJECT / GCLOUD_PROJECT.
  • For fully custom auth, use accessToken or accessTokenProvider directly.
  • Vertex model names are typically stable IDs without a date suffix (for example claude-sonnet-4-5).
  • Do not pass both apiKey and vertex at the same time.

Service account example:

import 'dart:convert';
import 'dart:io';

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

void main() async {
  final keyJson = jsonDecode(
    File('service-account.json').readAsStringSync(),
  );

  final ai = Genkit(
    plugins: [
      anthropic(
        vertex: AnthropicVertexConfig.serviceAccount(
          location: 'global',
          credentialsJson: keyJson,
        ),
      ),
    ],
  );

  final response = await ai.generate(
    model: anthropic.model('claude-sonnet-4-5'),
    prompt: 'Say hello from Vertex AI',
  );

  print(response.text);
}

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 => 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+) #

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 parts
print(response.message?.content);

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}');
0
likes
150
points
593
downloads

Publisher

verified publishergenkit.dev

Weekly Downloads

Anthropic plugin for Genkit Dart. Provides full support for the Claude family of models with advanced features like tool calling, multimodal inputs, and Genkit's unified generate API.

Repository (GitHub)
View/report issues
Contributing

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

anthropic_sdk_dart, genkit, genkit_vertex_auth, http, meta, schemantic

More

Packages that depend on genkit_anthropic