Skip to main content

ChatAnthropic

LangChain supports Anthropic's Claude family of chat models.

You'll first need to install the @langchain/anthropic package:

npm install @langchain/anthropic

You'll also need to sign up and obtain an Anthropic API key. Set it as an environment variable named ANTHROPIC_API_KEY, or pass it into the constructor as shown below.

Usage

You can initialize an instance like this:

import { ChatAnthropic } from "@langchain/anthropic";

const model = new ChatAnthropic({
temperature: 0.9,
modelName: "claude-3-sonnet-20240229",
// In Node.js defaults to process.env.ANTHROPIC_API_KEY,
// anthropicApiKey: "YOUR-API-KEY",
maxTokens: 1024,
});

const res = await model.invoke("Why is the sky blue?");

console.log(res);

/*
AIMessage {
content: "The sky appears blue because of how air in Earth's atmosphere interacts with sunlight. As sunlight passes through the atmosphere, light waves get scattered by gas molecules and airborne particles. Blue light waves scatter more easily than other color light waves. Since blue light gets scattered across the sky, we perceive the sky as having a blue color.",
name: undefined,
additional_kwargs: {
id: 'msg_01JuukTnjoXHuzQaPiSVvZQ1',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 15, output_tokens: 70 }
}
}
*/

API Reference:

Multimodal inputs

Claude-3 models support image multimodal inputs. The passed input must be a base64 encoded image with the filetype as a prefix (e.g. data:image/png;base64,{YOUR_BASE64_ENCODED_DATA}). Here's an example:

import * as fs from "node:fs/promises";

import { ChatAnthropic } from "@langchain/anthropic";
import { HumanMessage } from "@langchain/core/messages";

const imageData = await fs.readFile("./hotdog.jpg");
const chat = new ChatAnthropic({
modelName: "claude-3-sonnet-20240229",
});
const message = new HumanMessage({
content: [
{
type: "text",
text: "What's in this image?",
},
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${imageData.toString("base64")}`,
},
},
],
});

const res = await chat.invoke([message]);
console.log({ res });

/*
{
res: AIMessage {
content: 'The image shows a hot dog or frankfurter. It has a reddish-pink sausage filling encased in a light brown bun or bread roll. The hot dog is cut lengthwise, revealing the bright red sausage interior contrasted against the lightly toasted bread exterior. This classic fast food item is depicted in detail against a plain white background.',
name: undefined,
additional_kwargs: {
id: 'msg_0153boCaPL54QDEMQExkVur6',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: [Object]
}
}
}
*/

API Reference:

See the official docs for a complete list of supported file types.

Custom headers

You can pass custom headers in your requests like this:

import { ChatAnthropic } from "@langchain/anthropic";

const model = new ChatAnthropic({
modelName: "claude-3-sonnet-20240229",
maxTokens: 1024,
clientOptions: {
defaultHeaders: {
"X-Api-Key": process.env.ANTHROPIC_API_KEY,
},
},
});

const res = await model.invoke("Why is the sky blue?");

console.log(res);

/*
AIMessage {
content: "The sky appears blue because of the way sunlight interacts with the gases in Earth's atmosphere. Here's a more detailed explanation:\n" +
'\n' +
'- Sunlight is made up of different wavelengths of light, including the entire visible spectrum from red to violet.\n' +
'\n' +
'- As sunlight passes through the atmosphere, the gases (nitrogen, oxygen, etc.) cause the shorter wavelengths of light, in the blue and violet range, to be scattered more efficiently in different directions.\n' +
'\n' +
'- The blue wavelengths of about 475 nanometers get scattered more than the other visible wavelengths by the tiny gas molecules in the atmosphere.\n' +
'\n' +
'- This preferential scattering of blue light in all directions by the gas molecules is called Rayleigh scattering.\n' +
'\n' +
'- When we look at the sky, we see this scattered blue light from the sun coming at us from all parts of the sky.\n' +
'\n' +
"- At sunrise and sunset, the sun's rays have to travel further through the atmosphere before reaching our eyes, causing more of the blue light to be scattered out, leaving more of the red/orange wavelengths visible - which is why sunrises and sunsets appear reddish.\n" +
'\n' +
'So in summary, the blueness of the sky is caused by this selective scattering of blue wavelengths of sunlight by the gases in the atmosphere.',
name: undefined,
additional_kwargs: {
id: 'msg_01Mvvc5GvomqbUxP3YaeWXRe',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 13, output_tokens: 284 }
}
}
*/

API Reference:


Help us out by providing feedback on this documentation page: