!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

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
        *   [Memory](/v0.1/docs/modules/memory/)
            
        *   [Callbacks](/v0.1/docs/modules/callbacks/)
            
            *   [How-to](/v0.1/docs/modules/callbacks/how_to/background_callbacks/)
                
            *   [Callbacks](/v0.1/docs/modules/callbacks/)
        *   [Experimental](/v0.1/docs/modules/experimental/)
            
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Modules](/v0.1/docs/modules/)
*   More
*   Callbacks

On this page

Callbacks
=========

LangChain provides a callbacks system that allows you to hook into the various stages of your LLM application. This is useful for logging, monitoring, streaming, and other tasks.

You can subscribe to these events by using the `callbacks` argument available throughout the API. This method accepts a list of handler objects, which are expected to implement [one or more of the methods described in the API docs](https://api.js.langchain.com/interfaces/langchain_core_callbacks_base.CallbackHandlerMethods.html).

How to use callbacks[​](#how-to-use-callbacks "Direct link to How to use callbacks")
------------------------------------------------------------------------------------

The `callbacks` argument is available on most objects throughout the API ([Chains](/v0.1/docs/modules/chains/), [Language Models](/v0.1/docs/modules/model_io/), [Tools](/v0.1/docs/modules/agents/tools/), [Agents](/v0.1/docs/modules/agents/), etc.) in two different places.

### Constructor callbacks[​](#constructor-callbacks "Direct link to Constructor callbacks")

Defined in the constructor, eg. `new LLMChain({ callbacks: [handler] })`, which will be used for all calls made on that object, and will be scoped to that object only, eg. if you pass a handler to the `LLMChain` constructor, it will not be used by the Model attached to that chain.

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/openai @langchain/core

    yarn add @langchain/openai @langchain/core

    pnpm add @langchain/openai @langchain/core

    import { OpenAI } from "@langchain/openai";import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";const llm = new OpenAI({  temperature: 0,  // These tags will be attached to all calls made with this LLM.  tags: ["example", "callbacks", "constructor"],  // This handler will be used for all calls made with this LLM.  callbacks: [new ConsoleCallbackHandler()],});

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [ConsoleCallbackHandler](https://api.js.langchain.com/classes/langchain_core_tracers_console.ConsoleCallbackHandler.html) from `@langchain/core/tracers/console`

### Request callbacks[​](#request-callbacks "Direct link to Request callbacks")

Defined in the `call()`/`run()`/`apply()` methods used for issuing a request, eg. `chain.call({ input: '...' }, [handler])`, which will be used for that specific request only, and all sub-requests that it contains (eg. a call to an LLMChain triggers a call to a Model, which uses the same handler passed in the `call()` method).

    import { OpenAI } from "@langchain/openai";import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";const llm = new OpenAI({  temperature: 0,});const response = await llm.invoke("1 + 1 =", {  // These tags will be attached only to this call to the LLM.  tags: ["example", "callbacks", "request"],  // This handler will be used only for this call.  callbacks: [new ConsoleCallbackHandler()],});

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [ConsoleCallbackHandler](https://api.js.langchain.com/classes/langchain_core_tracers_console.ConsoleCallbackHandler.html) from `@langchain/core/tracers/console`

### Verbose mode[​](#verbose-mode "Direct link to Verbose mode")

The `verbose` argument is available on most objects throughout the API (Chains, Models, Tools, Agents, etc.) as a constructor argument, eg. `new LLMChain({ verbose: true })`, and it is equivalent to passing a `ConsoleCallbackHandler` to the `callbacks` argument of that object and all child objects. This is useful for debugging, as it will log all events to the console. You can also enable verbose mode for the entire application by setting the environment variable `LANGCHAIN_VERBOSE=true`.

    import { LLMChain } from "langchain/chains";import { OpenAI } from "@langchain/openai";import { PromptTemplate } from "@langchain/core/prompts";const chain = new LLMChain({  llm: new OpenAI({ temperature: 0 }),  prompt: PromptTemplate.fromTemplate("Hello, world!"),  // This will enable logging of all Chain *and* LLM events to the console.  verbose: true,});

#### API Reference:

*   [LLMChain](https://api.js.langchain.com/classes/langchain_chains.LLMChain.html) from `langchain/chains`
*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

### When do you want to use each of these?[​](#when-do-you-want-to-use-each-of-these "Direct link to When do you want to use each of these?")

*   Constructor callbacks are most useful for use cases such as logging, monitoring, etc., which are _not specific to a single request_, but rather to the entire chain. For example, if you want to log all the requests made to an LLMChain, you would pass a handler to the constructor.
*   Request callbacks are most useful for use cases such as streaming, where you want to stream the output of a single request to a specific websocket connection, or other similar use cases. For example, if you want to stream the output of a single request to a websocket, you would pass a handler to the `call()` method

Usage examples[​](#usage-examples "Direct link to Usage examples")
------------------------------------------------------------------

### Built-in handlers[​](#built-in-handlers "Direct link to Built-in handlers")

LangChain provides a few built-in handlers that you can use to get started. These are available in the `langchain/callbacks` module. The most basic handler is the `ConsoleCallbackHandler`, which simply logs all events to the console. In the future we will add more default handlers to the library. Note that when the `verbose` flag on the object is set to `true`, the `ConsoleCallbackHandler` will be invoked even without being explicitly passed in.

    import { LLMChain } from "langchain/chains";import { OpenAI } from "@langchain/openai";import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";import { PromptTemplate } from "@langchain/core/prompts";export const run = async () => {  const handler = new ConsoleCallbackHandler();  const llm = new OpenAI({ temperature: 0, callbacks: [handler] });  const prompt = PromptTemplate.fromTemplate("1 + {number} =");  const chain = new LLMChain({ prompt, llm, callbacks: [handler] });  const output = await chain.invoke({ number: 2 });  /*  Entering new llm_chain chain...  Finished chain.  */  console.log(output);  /*  { text: ' 3\n\n3 - 1 = 2' }   */  // The non-enumerable key `__run` contains the runId.  console.log(output.__run);  /*  { runId: '90e1f42c-7cb4-484c-bf7a-70b73ef8e64b' }  */};

#### API Reference:

*   [LLMChain](https://api.js.langchain.com/classes/langchain_chains.LLMChain.html) from `langchain/chains`
*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [ConsoleCallbackHandler](https://api.js.langchain.com/classes/langchain_core_tracers_console.ConsoleCallbackHandler.html) from `@langchain/core/tracers/console`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`

### One-off handlers[​](#one-off-handlers "Direct link to One-off handlers")

You can create a one-off handler inline by passing a plain object to the `callbacks` argument. This object should implement the [`CallbackHandlerMethods`](https://api.js.langchain.com/interfaces/langchain_core_callbacks_base.CallbackHandlerMethods.html) interface. This is useful if eg. you need to create a handler that you will use only for a single request, eg to stream the output of an LLM/Agent/etc to a websocket.

    import { OpenAI } from "@langchain/openai";// To enable streaming, we pass in `streaming: true` to the LLM constructor.// Additionally, we pass in a handler for the `handleLLMNewToken` event.const model = new OpenAI({  maxTokens: 25,  streaming: true,});const response = await model.invoke("Tell me a joke.", {  callbacks: [    {      handleLLMNewToken(token: string) {        console.log({ token });      },    },  ],});console.log(response);/*{ token: '\n' }{ token: '\n' }{ token: 'Q' }{ token: ':' }{ token: ' Why' }{ token: ' did' }{ token: ' the' }{ token: ' chicken' }{ token: ' cross' }{ token: ' the' }{ token: ' playground' }{ token: '?' }{ token: '\n' }{ token: 'A' }{ token: ':' }{ token: ' To' }{ token: ' get' }{ token: ' to' }{ token: ' the' }{ token: ' other' }{ token: ' slide' }{ token: '.' }Q: Why did the chicken cross the playground?A: To get to the other slide.*/

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`

### Multiple handlers[​](#multiple-handlers "Direct link to Multiple handlers")

We offer a method on the `CallbackManager` class that allows you to create a one-off handler. This is useful if eg. you need to create a handler that you will use only for a single request.

tip

Agents now have built in streaming support! Click [here](/v0.1/docs/modules/agents/how_to/streaming/) for more details.

This is a more complete example that passes a `CallbackManager` to a ChatModel, and LLMChain, a Tool, and an Agent.

    import { LLMChain } from "langchain/chains";import { AgentExecutor, ZeroShotAgent } from "langchain/agents";import { ChatOpenAI } from "@langchain/openai";import { Calculator } from "@langchain/community/tools/calculator";import { Serialized } from "@langchain/core/load/serializable";import { BaseCallbackHandler } from "@langchain/core/callbacks/base";import { AgentAction } from "@langchain/core/agents";export const run = async () => {  // You can implement your own callback handler by extending BaseCallbackHandler  class CustomHandler extends BaseCallbackHandler {    name = "custom_handler";    handleLLMNewToken(token: string) {      console.log("token", { token });    }    handleLLMStart(llm: Serialized, _prompts: string[]) {      console.log("handleLLMStart", { llm });    }    handleChainStart(chain: Serialized) {      console.log("handleChainStart", { chain });    }    handleAgentAction(action: AgentAction) {      console.log("handleAgentAction", action);    }    handleToolStart(tool: Serialized) {      console.log("handleToolStart", { tool });    }  }  const handler1 = new CustomHandler();  // Additionally, you can use the `fromMethods` method to create a callback handler  const handler2 = BaseCallbackHandler.fromMethods({    handleLLMStart(llm, _prompts: string[]) {      console.log("handleLLMStart: I'm the second handler!!", { llm });    },    handleChainStart(chain) {      console.log("handleChainStart: I'm the second handler!!", { chain });    },    handleAgentAction(action) {      console.log("handleAgentAction", action);    },    handleToolStart(tool) {      console.log("handleToolStart", { tool });    },  });  // You can restrict callbacks to a particular object by passing it upon creation  const model = new ChatOpenAI({    temperature: 0,    callbacks: [handler2], // this will issue handler2 callbacks related to this model    streaming: true, // needed to enable streaming, which enables handleLLMNewToken  });  const tools = [new Calculator()];  const agentPrompt = ZeroShotAgent.createPrompt(tools);  const llmChain = new LLMChain({    llm: model,    prompt: agentPrompt,    callbacks: [handler2], // this will issue handler2 callbacks related to this chain  });  const agent = new ZeroShotAgent({    llmChain,    allowedTools: ["search"],  });  const agentExecutor = AgentExecutor.fromAgentAndTools({    agent,    tools,  });  /*   * When we pass the callback handler to the agent executor, it will be used for all   * callbacks related to the agent and all the objects involved in the agent's   * execution, in this case, the Tool, LLMChain, and LLM.   *   * The `handler2` callback handler will only be used for callbacks related to the   * LLMChain and LLM, since we passed it to the LLMChain and LLM objects upon creation.   */  const result = await agentExecutor.invoke(    {      input: "What is 2 to the power of 8",    },    { callbacks: [handler1] }  ); // this is needed to see handleAgentAction  /*  handleChainStart { chain: { name: 'agent_executor' } }  handleChainStart { chain: { name: 'llm_chain' } }  handleChainStart: I'm the second handler!! { chain: { name: 'llm_chain' } }  handleLLMStart { llm: { name: 'openai' } }  handleLLMStart: I'm the second handler!! { llm: { name: 'openai' } }  token { token: '' }  token { token: 'I' }  token { token: ' can' }  token { token: ' use' }  token { token: ' the' }  token { token: ' calculator' }  token { token: ' tool' }  token { token: ' to' }  token { token: ' solve' }  token { token: ' this' }  token { token: '.\n' }  token { token: 'Action' }  token { token: ':' }  token { token: ' calculator' }  token { token: '\n' }  token { token: 'Action' }  token { token: ' Input' }  token { token: ':' }  token { token: ' ' }  token { token: '2' }  token { token: '^' }  token { token: '8' }  token { token: '' }  handleAgentAction {    tool: 'calculator',    toolInput: '2^8',    log: 'I can use the calculator tool to solve this.\n' +      'Action: calculator\n' +      'Action Input: 2^8'  }  handleToolStart { tool: { name: 'calculator' } }  handleChainStart { chain: { name: 'llm_chain' } }  handleChainStart: I'm the second handler!! { chain: { name: 'llm_chain' } }  handleLLMStart { llm: { name: 'openai' } }  handleLLMStart: I'm the second handler!! { llm: { name: 'openai' } }  token { token: '' }  token { token: 'That' }  token { token: ' was' }  token { token: ' easy' }  token { token: '!\n' }  token { token: 'Final' }  token { token: ' Answer' }  token { token: ':' }  token { token: ' ' }  token { token: '256' }  token { token: '' }  */  console.log(result);  /*  {    output: '256',    __run: { runId: '26d481a6-4410-4f39-b74d-f9a4f572379a' }  }  */};

#### API Reference:

*   [LLMChain](https://api.js.langchain.com/classes/langchain_chains.LLMChain.html) from `langchain/chains`
*   [AgentExecutor](https://api.js.langchain.com/classes/langchain_agents.AgentExecutor.html) from `langchain/agents`
*   [ZeroShotAgent](https://api.js.langchain.com/classes/langchain_agents.ZeroShotAgent.html) from `langchain/agents`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [Calculator](https://api.js.langchain.com/classes/langchain_community_tools_calculator.Calculator.html) from `@langchain/community/tools/calculator`
*   [Serialized](https://api.js.langchain.com/types/langchain_core_load_serializable.Serialized.html) from `@langchain/core/load/serializable`
*   [BaseCallbackHandler](https://api.js.langchain.com/classes/langchain_core_callbacks_base.BaseCallbackHandler.html) from `@langchain/core/callbacks/base`
*   [AgentAction](https://api.js.langchain.com/types/langchain_core_agents.AgentAction.html) from `@langchain/core/agents`

* * *

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

[

Previous

Vector store-backed memory

](/v0.1/docs/modules/memory/types/vectorstore_retriever_memory/)[

Next

Backgrounding callbacks

](/v0.1/docs/modules/callbacks/how_to/background_callbacks/)

*   [How to use callbacks](#how-to-use-callbacks)
    *   [Constructor callbacks](#constructor-callbacks)
    *   [Request callbacks](#request-callbacks)
    *   [Verbose mode](#verbose-mode)
    *   [When do you want to use each of these?](#when-do-you-want-to-use-each-of-these)
*   [Usage examples](#usage-examples)
    *   [Built-in handlers](#built-in-handlers)
    *   [One-off handlers](#one-off-handlers)
    *   [Multiple handlers](#multiple-handlers)

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.