@gensx/anthropic
A pre-wrapped version of the Anthropic SDK for GenSX. This package provides a simple way to use Anthropic's API with GenSX components.
Installation
npm install @gensx/anthropic
Usage
You can use this package in two ways:
1. Drop-in Replacement (Recommended)
Simply replace your Anthropic import with the GenSX version:
import { Anthropic } from "@gensx/anthropic";
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const completion = await client.messages.create({
model: "claude-sonnet-4-20250514",
messages: [{ role: "user", content: "Hello!" }],
max_tokens: 1000,
});
const stream = await client.messages.create({
model: "claude-sonnet-4-20250514",
messages: [{ role: "user", content: "Hello!" }],
max_tokens: 1000,
stream: true,
});
2. Wrap an Existing Instance
If you already have an Anthropic instance, you can wrap it with GenSX functionality:
import { Anthropic } from "@anthropic-ai/sdk";
import { wrapAnthropic } from "@gensx/anthropic";
const client = wrapAnthropic(
new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
}),
);
const completion = await client.messages.create({
model: "claude-sonnet-4-20250514",
messages: [{ role: "user", content: "Hello!" }],
max_tokens: 1000,
});
3. Using with GenSX Components
You can also use the package with GenSX components:
import * as gensx from "@gensx/core";
import { AnthropicProvider, ChatCompletion } from "@gensx/anthropic";
const ChatBot = gensx.Component(async ({ userInput }) => {
return (
<AnthropicProvider apiKey={process.env.ANTHROPIC_API_KEY!}>
<ChatCompletion
system="You are a helpful assistant."
messages={[{ role: "user", content: userInput }]}
model="claude-sonnet-4-20250514"
temperature={0.7}
/>
</AnthropicProvider>
);
});
const StreamingChat = gensx.Component(async ({ userInput }) => {
return (
<AnthropicProvider apiKey={process.env.ANTHROPIC_API_KEY!}>
<ChatCompletion
system="You are a helpful assistant."
messages={[{ role: "user", content: userInput }]}
stream={true}
model="claude-sonnet-4-20250514"
>
{async (stream) => {
for await (const token of stream) {
process.stdout.write(token);
}
}}
</ChatCompletion>
</AnthropicProvider>
);
});
API
The package exports:
Anthropic
- A drop-in replacement for the Anthropic client that automatically wraps all methods with GenSX functionality
wrapAnthropic
- A function to manually wrap an Anthropic instance with GenSX functionality
AnthropicProvider
- A GenSX component for providing Anthropic context
ChatCompletion
- A GenSX component for creating chat completions
All methods from the Anthropic SDK are supported and automatically wrapped with GenSX functionality.
License
Apache-2.0