!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
        *   [Chat models](/v0.1/docs/integrations/chat/)
        *   [Alibaba Tongyi](/v0.1/docs/integrations/chat/alibaba_tongyi/)
        *   [Anthropic](/v0.1/docs/integrations/chat/anthropic/)
        *   [Anthropic Tools](/v0.1/docs/integrations/chat/anthropic_tools/)
        *   [Azure OpenAI](/v0.1/docs/integrations/chat/azure/)
        *   [Baidu Wenxin](/v0.1/docs/integrations/chat/baidu_wenxin/)
        *   [Bedrock](/v0.1/docs/integrations/chat/bedrock/)
        *   [Cloudflare Workers AI](/v0.1/docs/integrations/chat/cloudflare_workersai/)
        *   [Cohere](/v0.1/docs/integrations/chat/cohere/)
        *   [Fake LLM](/v0.1/docs/integrations/chat/fake/)
        *   [Fireworks](/v0.1/docs/integrations/chat/fireworks/)
        *   [Friendli](/v0.1/docs/integrations/chat/friendli/)
        *   [Google GenAI](/v0.1/docs/integrations/chat/google_generativeai/)
        *   [(Legacy) Google PaLM/VertexAI](/v0.1/docs/integrations/chat/google_palm/)
        *   [Google Vertex AI](/v0.1/docs/integrations/chat/google_vertex_ai/)
        *   [Groq](/v0.1/docs/integrations/chat/groq/)
        *   [Llama CPP](/v0.1/docs/integrations/chat/llama_cpp/)
        *   [Minimax](/v0.1/docs/integrations/chat/minimax/)
        *   [Mistral AI](/v0.1/docs/integrations/chat/mistral/)
        *   [NIBittensorChatModel](/v0.1/docs/integrations/chat/ni_bittensor/)
        *   [Ollama](/v0.1/docs/integrations/chat/ollama/)
        *   [Ollama Functions](/v0.1/docs/integrations/chat/ollama_functions/)
        *   [OpenAI](/v0.1/docs/integrations/chat/openai/)
        *   [PremAI](/v0.1/docs/integrations/chat/premai/)
        *   [PromptLayer OpenAI](/v0.1/docs/integrations/chat/prompt_layer_openai/)
        *   [TogetherAI](/v0.1/docs/integrations/chat/togetherai/)
        *   [WebLLM](/v0.1/docs/integrations/chat/web_llm/)
        *   [YandexGPT](/v0.1/docs/integrations/chat/yandex/)
        *   [ZhipuAI](/v0.1/docs/integrations/chat/zhipuai/)
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Chat models](/v0.1/docs/integrations/chat/)
*   Anthropic Tools

On this page

anthropic\_tools
================

danger

This API is deprecated as Anthropic now officially supports tools. [Click here to read the documentation](/v0.1/docs/integrations/chat/anthropic/#tools).

Anthropic Tools
===============

LangChain offers an experimental wrapper around Anthropic that gives it the same API as OpenAI Functions.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

To start, install the `@langchain/anthropic` integration package.

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/anthropic

    yarn add @langchain/anthropic

    pnpm add @langchain/anthropic

Initialize model[​](#initialize-model "Direct link to Initialize model")
------------------------------------------------------------------------

You can initialize this wrapper the same way you'd initialize a standard `ChatAnthropic` instance:

tip

We're unifying model params across all packages. We now suggest using `model` instead of `modelName`, and `apiKey` for API keys.

    import { ChatAnthropicTools } from "@langchain/anthropic/experimental";const model = new ChatAnthropicTools({  temperature: 0.1,  model: "claude-3-sonnet-20240229",  apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.ANTHROPIC_API_KEY});

Passing in tools[​](#passing-in-tools "Direct link to Passing in tools")
------------------------------------------------------------------------

You can now pass in tools the same way as OpenAI:

    import { ChatAnthropicTools } from "@langchain/anthropic/experimental";import { HumanMessage } from "@langchain/core/messages";const model = new ChatAnthropicTools({  temperature: 0.1,  model: "claude-3-sonnet-20240229",}).bind({  tools: [    {      type: "function",      function: {        name: "get_current_weather",        description: "Get the current weather in a given location",        parameters: {          type: "object",          properties: {            location: {              type: "string",              description: "The city and state, e.g. San Francisco, CA",            },            unit: { type: "string", enum: ["celsius", "fahrenheit"] },          },          required: ["location"],        },      },    },  ],  // You can set the `function_call` arg to force the model to use a function  tool_choice: {    type: "function",    function: {      name: "get_current_weather",    },  },});const response = await model.invoke([  new HumanMessage({    content: "What's the weather in Boston?",  }),]);console.log(response);/*  AIMessage {    lc_serializable: true,    lc_kwargs: { content: '', additional_kwargs: { tool_calls: [Array] } },    lc_namespace: [ 'langchain_core', 'messages' ],    content: '',    name: undefined,    additional_kwargs: { tool_calls: [ [Object] ] }  }*/console.log(response.additional_kwargs.tool_calls);/*  [    {      id: '0',      type: 'function',      function: {        name: 'get_current_weather',        arguments: '{"location":"Boston, MA","unit":"fahrenheit"}'      }    }  ]*/

#### API Reference:

*   [ChatAnthropicTools](https://api.js.langchain.com/classes/langchain_anthropic_experimental.ChatAnthropicTools.html) from `@langchain/anthropic/experimental`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`

Parallel tool calling[​](#parallel-tool-calling "Direct link to Parallel tool calling")
---------------------------------------------------------------------------------------

The model may choose to call multiple tools. Here is an example using an extraction use-case:

    import { z } from "zod";import { zodToJsonSchema } from "zod-to-json-schema";import { ChatAnthropicTools } from "@langchain/anthropic/experimental";import { JsonOutputToolsParser } from "langchain/output_parsers";import { PromptTemplate } from "@langchain/core/prompts";const EXTRACTION_TEMPLATE = `Extract and save the relevant entities mentioned in the following passage together with their properties.Passage:{input}`;const prompt = PromptTemplate.fromTemplate(EXTRACTION_TEMPLATE);// Use Zod for easier schema declarationconst schema = z.object({  name: z.string().describe("The name of a person"),  height: z.number().describe("The person's height"),  hairColor: z.optional(z.string()).describe("The person's hair color"),});const model = new ChatAnthropicTools({  temperature: 0.1,  model: "claude-3-sonnet-20240229",}).bind({  tools: [    {      type: "function",      function: {        name: "person",        description: "Extracts the relevant people from the passage.",        parameters: zodToJsonSchema(schema),      },    },  ],  // Can also set to "auto" to let the model choose a tool  tool_choice: {    type: "function",    function: {      name: "person",    },  },});// Use a JsonOutputToolsParser to get the parsed JSON response directly.const chain = await prompt.pipe(model).pipe(new JsonOutputToolsParser());const response = await chain.invoke({  input:    "Alex is 5 feet tall. Claudia is 1 foot taller than Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.",});console.log(JSON.stringify(response, null, 2));/*  [    {      "type": "person",      "args": {        "name": "Alex",        "height": 5,        "hairColor": "blonde"      }    },    {      "type": "person",      "args": {        "name": "Claudia",        "height": 6,        "hairColor": "brunette"      }    }  ]*/

#### API Reference:

*   [ChatAnthropicTools](https://api.js.langchain.com/classes/langchain_anthropic_experimental.ChatAnthropicTools.html) from `@langchain/anthropic/experimental`
*   [JsonOutputToolsParser](https://api.js.langchain.com/classes/langchain_output_parsers.JsonOutputToolsParser.html) from `langchain/output_parsers`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

`.withStructuredOutput({ ... })`[​](#withstructuredoutput-- "Direct link to withstructuredoutput--")
----------------------------------------------------------------------------------------------------

info

The `.withStructuredOutput` method is in beta. It is actively being worked on, so the API may change.

Using the `.withStructuredOutput` method, you can make the LLM return structured output, given only a Zod or JSON schema:

    import { z } from "zod";import { ChatAnthropicTools } from "@langchain/anthropic/experimental";import { ChatPromptTemplate } from "@langchain/core/prompts";const calculatorSchema = z.object({  operation: z    .enum(["add", "subtract", "multiply", "divide"])    .describe("The type of operation to execute"),  number1: z.number().describe("The first number to operate on."),  number2: z.number().describe("The second number to operate on."),});const model = new ChatAnthropicTools({  model: "claude-3-sonnet-20240229",  temperature: 0.1,});// Pass the schema and tool name to the withStructuredOutput methodconst modelWithTool = model.withStructuredOutput(calculatorSchema);// You can also set force: false to allow the model scratchpad space.// This may improve reasoning capabilities.// const modelWithTool = model.withStructuredOutput(calculatorSchema, {//   force: false,// });const prompt = ChatPromptTemplate.fromMessages([  [    "system",    "You are a helpful assistant who always needs to use a calculator.",  ],  ["human", "{input}"],]);// Chain your prompt and model togetherconst chain = prompt.pipe(modelWithTool);const response = await chain.invoke({  input: "What is 2 + 2?",});console.log(response);/*  { operation: 'add', number1: 2, number2: 2 }*/

#### API Reference:

*   [ChatAnthropicTools](https://api.js.langchain.com/classes/langchain_anthropic_experimental.ChatAnthropicTools.html) from `@langchain/anthropic/experimental`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`

### Using JSON schema:[​](#using-json-schema "Direct link to Using JSON schema:")

    import { ChatAnthropicTools } from "@langchain/anthropic/experimental";import { ChatPromptTemplate } from "@langchain/core/prompts";const calculatorJsonSchema = {  type: "object",  properties: {    operation: {      type: "string",      enum: ["add", "subtract", "multiply", "divide"],      description: "The type of operation to execute.",    },    number1: { type: "number", description: "The first number to operate on." },    number2: {      type: "number",      description: "The second number to operate on.",    },  },  required: ["operation", "number1", "number2"],  description: "A simple calculator tool",};const model = new ChatAnthropicTools({  model: "claude-3-sonnet-20240229",  temperature: 0.1,});// Pass the schema and optionally, the tool name to the withStructuredOutput methodconst modelWithTool = model.withStructuredOutput(calculatorJsonSchema, {  name: "calculator",});const prompt = ChatPromptTemplate.fromMessages([  [    "system",    "You are a helpful assistant who always needs to use a calculator.",  ],  ["human", "{input}"],]);// Chain your prompt and model togetherconst chain = prompt.pipe(modelWithTool);const response = await chain.invoke({  input: "What is 2 + 2?",});console.log(response);/*  { operation: 'add', number1: 2, number2: 2 }*/

#### API Reference:

*   [ChatAnthropicTools](https://api.js.langchain.com/classes/langchain_anthropic_experimental.ChatAnthropicTools.html) from `@langchain/anthropic/experimental`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Anthropic

](/v0.1/docs/integrations/chat/anthropic/)[

Next

Azure OpenAI

](/v0.1/docs/integrations/chat/azure/)

*   [Setup](#setup)
*   [Initialize model](#initialize-model)
*   [Passing in tools](#passing-in-tools)
*   [Parallel tool calling](#parallel-tool-calling)
*   [`.withStructuredOutput({ ... })`](#withstructuredoutput--)
    *   [Using JSON schema:](#using-json-schema)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.