Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions js/plugins/anthropic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib/
node_modules/
coverage/
17 changes: 17 additions & 0 deletions js/plugins/anthropic/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# typescript source files
src/
tests/
tsconfig.json
tsup.common.ts
tsup.config.ts

# GitHub files
.github/
.gitignore
.npmignore
CODE_OF_CONDUCT.md
CONTRIBUTING.md

# Developer related files
.devcontainer/
.vscode/
8 changes: 8 additions & 0 deletions js/plugins/anthropic/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This project includes code derived from the Firebase Genkit Anthropic community plugin
(https://github.com/BloomLabsInc/genkit-plugins/tree/main/plugins/anthropic).

Copyright 2024 Bloom Labs Inc.
Copyright 2025 Google LLC.

Licensed under the Apache License, Version 2.0.
See the LICENSE file distributed with this project for the full license text.
195 changes: 195 additions & 0 deletions js/plugins/anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# Firebase Genkit + Anthropic AI

<h1 align="center">Firebase Genkit <> Anthropic AI Plugin</h1>

<h4 align="center">Anthropic AI plugin for Google Firebase Genkit</h4>

`@genkit-ai/anthropic` is the official Anthropic plugin for [Firebase Genkit](https://github.com/firebase/genkit). It supersedes the earlier community package `genkitx-anthropic` and is now maintained by Google.

## Supported models

The plugin supports the most recent Anthropic models: **Claude Sonnet 4.5**, **Claude Opus 4.1**, **Claude Haiku 4.5**, **Claude Sonnet 4**, **Claude Opus 4**, **Claude 3.5 Haiku**, and **Claude 3 Haiku**.

## Installation

Install the plugin in your project with your favorite package manager:

- `npm install @genkit-ai/anthropic`
- `yarn add @genkit-ai/anthropic`
- `pnpm add @genkit-ai/anthropic`

## Usage

### Initialize

```typescript
import { genkit } from 'genkit';
import { anthropic } from '@genkit-ai/anthropic';

const ai = genkit({
plugins: [anthropic({ apiKey: process.env.ANTHROPIC_API_KEY })],
// specify a default model for generate here if you wish:
model: anthropic.model('claude-sonnet-4-5'),
});
```

### Basic examples

The simplest way to generate text is by using the `generate` method:

```typescript
const response = await ai.generate({
model: anthropic.model('claude-3-haiku'),
prompt: 'Tell me a joke.',
});

console.log(response.text);
```

### Multi-modal prompt

```typescript
// ...initialize Genkit instance (as shown above)...

const response = await ai.generate({
prompt: [
{ text: 'What animal is in the photo?' },
{ media: { url: imageUrl } },
],
config: {
// control of the level of visual detail when processing image embeddings
// Low detail level also decreases the token usage
visualDetailLevel: 'low',
},
});
console.log(response.text);
```

### Extended thinking

Claude 4 models can expose their internal reasoning. Enable it per-request with the Anthropic thinking config and read the reasoning from the response:

```typescript
const response = await ai.generate({
prompt: 'Walk me through your reasoning for Fermat’s little theorem.',
config: {
thinking: {
enabled: true,
budgetTokens: 4096, // Must be >= 1024 and less than max_tokens
},
},
});

console.log(response.text); // Final assistant answer
console.log(response.reasoning); // Summarized thinking steps
```

When thinking is enabled, request bodies sent through the plugin include the `thinking` payload (`{ type: 'enabled', budget_tokens: … }`) that Anthropic's API expects, and streamed responses deliver `reasoning` parts as they arrive so you can render the chain-of-thought incrementally.

### Beta API Limitations

The beta API surface provides access to experimental features, but some server-managed tool blocks are not yet supported by this plugin. The following beta API features will cause an error if encountered:

- `web_fetch_tool_result`
- `code_execution_tool_result`
- `bash_code_execution_tool_result`
- `text_editor_code_execution_tool_result`
- `mcp_tool_result`
- `mcp_tool_use`
- `container_upload`

Note that `server_tool_use` and `web_search_tool_result` ARE supported and work with both stable and beta APIs.

### Within a flow

```typescript
import { z } from 'genkit';

// ...initialize Genkit instance (as shown above)...

export const jokeFlow = ai.defineFlow(
{
name: 'jokeFlow',
inputSchema: z.string(),
outputSchema: z.string(),
},
async (subject) => {
const llmResponse = await ai.generate({
prompt: `tell me a joke about ${subject}`,
});
return llmResponse.text;
}
);
```

### Direct model usage (without Genkit instance)

The plugin supports Genkit Plugin API v2, which allows you to use models directly without initializing the full Genkit framework:

```typescript
import { anthropic } from '@genkit-ai/anthropic';

// Create a model reference directly
const claude = anthropic.model('claude-sonnet-4-5');

// Use the model directly
const response = await claude({
messages: [
{
role: 'user',
content: [{ text: 'Tell me a joke.' }],
},
],
});

console.log(response);
```

You can also create model references using the plugin's `model()` method:

```typescript
import { anthropic } from '@genkit-ai/anthropic';

// Create model references
const claudeSonnet45 = anthropic.model('claude-sonnet-4-5');
const claudeOpus41 = anthropic.model('claude-opus-4-1');
const claude35Haiku = anthropic.model('claude-3-5-haiku');

// Use the model reference directly
const response = await claudeSonnet45({
messages: [
{
role: 'user',
content: [{ text: 'Hello!' }],
},
],
});
```

This approach is useful for:

- Framework developers who need raw model access
- Testing models in isolation
- Using Genkit models in non-Genkit applications

## Acknowledgements

This plugin builds on the community work published as [`genkitx-anthropic`](https://github.com/BloomLabsInc/genkit-plugins/blob/main/plugins/anthropic/README.md) by Bloom Labs Inc. Their Apache 2.0–licensed implementation provided the foundation for this maintained package.

## Contributing

Want to contribute to the project? That's awesome! Head over to our [Contribution Guidelines](CONTRIBUTING.md).

## Need support?

> [!NOTE]
> This repository depends on Google's Firebase Genkit. For issues and questions related to Genkit, please refer to instructions available in [Genkit's repository](https://github.com/firebase/genkit).


## Credits

This plugin is maintained by Google with acknowledgement to the community contributions from [Bloom Labs Inc](https://github.com/BloomLabsInc).

## License

This project is licensed under the [Apache 2.0 License](https://github.com/BloomLabsInc/genkit-plugins/blob/main/LICENSE).
70 changes: 70 additions & 0 deletions js/plugins/anthropic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "@genkit-ai/anthropic",
"description": "Genkit AI framework plugin for Anthropic APIs.",
"keywords": [
"genkit",
"genkit-plugin",
"genkit-model",
"anthropic",
"anthropic-ai",
"claude-4",
"haiku-4",
"opus",
"haiku",
"sonnet",
"ai",
"genai",
"generative-ai"
],
"version": "1.21.0",
"type": "commonjs",
"repository": {
"type": "git",
"url": "https://github.com/firebase/genkit.git",
"directory": "js/plugins/anthropic"
},
"author": "genkit",
"license": "Apache-2.0",
"peerDependencies": {
"genkit": "workspace:^"
},
"dependencies": {
"@anthropic-ai/sdk": "^0.68.0"
},
"devDependencies": {
"@types/node": "^20.11.16",
"check-node-version": "^4.2.1",
"genkit": "workspace:*",
"npm-run-all": "^4.1.5",
"rimraf": "^6.0.1",
"tsup": "^8.3.5",
"tsx": "^4.19.2",
"typescript": "^4.9.0"
},
"types": "./lib/index.d.ts",
"exports": {
".": {
"types": "./lib/index.d.ts",
"require": "./lib/index.js",
"import": "./lib/index.mjs",
"default": "./lib/index.js"
}
},
"files": [
"lib"
],
"publishConfig": {
"provenance": true,
"access": "public"
},
"scripts": {
"check": "tsc",
"compile": "tsup-node",
"build:clean": "rimraf ./lib",
"build": "npm-run-all build:clean check compile",
"build:watch": "tsup-node --watch",
"test": "tsx --test tests/*_test.ts",
"test:file": "tsx --test",
"test:coverage": "check-node-version --node '>=22' && tsx --test --experimental-test-coverage --test-coverage-include='src/**/*.ts' ./tests/**/*_test.ts"
}
}
Loading
Loading