*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to create custom callback handlers

On this page

How to create custom callback handlers
======================================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Callbacks](/v0.2/docs/concepts/#callbacks)

LangChain has some built-in callback handlers, but you will often want to create your own handlers with custom logic.

To create a custom callback handler, we need to determine the [event(s)](https://api.js.langchain.com/interfaces/langchain_core_callbacks_base.CallbackHandlerMethods.html) we want our callback handler to handle as well as what we want our callback handler to do when the event is triggered. Then all we need to do is attach the callback handler to the object, for example via [the constructor](/v0.2/docs/how_to/callbacks_constructor) or [at runtime](/v0.2/docs/how_to/callbacks_runtime).

An easy way to construct a custom callback handler is to initialize it as an object whose keys are functions with names matching the events we want to handle. Here’s an example that only handles the start of a chat model and streamed tokens from the model run:

    import { ChatPromptTemplate } from "@langchain/core/prompts";import { ChatAnthropic } from "@langchain/anthropic";const prompt = ChatPromptTemplate.fromTemplate(`What is 1 + {number}?`);const model = new ChatAnthropic({  model: "claude-3-sonnet-20240229",});const chain = prompt.pipe(model);const customHandler = {  handleChatModelStart: async (llm, inputMessages, runId) => {    console.log("Chat model start:", llm, inputMessages, runId);  },  handleLLMNewToken: async (token) => {    console.log("Chat model new token", token);  },};const stream = await chain.stream(  { number: "2" },  { callbacks: [customHandler] });for await (const _ of stream) {  // Just consume the stream so the callbacks run}

    Chat model start: {  lc: 1,  type: "constructor",  id: [ "langchain", "chat_models", "anthropic", "ChatAnthropic" ],  kwargs: {    callbacks: undefined,    model: "claude-3-sonnet-20240229",    verbose: undefined,    anthropic_api_key: { lc: 1, type: "secret", id: [ "ANTHROPIC_API_KEY" ] },    api_key: { lc: 1, type: "secret", id: [ "ANTHROPIC_API_KEY" ] }  }} [  [    HumanMessage {      lc_serializable: true,      lc_kwargs: {        content: "What is 1 + 2?",        additional_kwargs: {},        response_metadata: {}      },      lc_namespace: [ "langchain_core", "messages" ],      content: "What is 1 + 2?",      name: undefined,      additional_kwargs: {},      response_metadata: {}    }  ]] b6e3b7ad-c602-4cef-9652-d51781a657b7Chat model new token TheChat model new token  sumChat model new token  ofChat model new tokenChat model new token 1Chat model new tokenChat model new token anChat model new token dChat model new token 2Chat model new tokenChat model new token isChat model new tokenChat model new token 3Chat model new token .

You can see [this reference page](https://api.js.langchain.com/interfaces/langchain_core_callbacks_base.CallbackHandlerMethods.html) for a list of events you can handle. Note that the `handleChain*` events run for most LCEL runnables.

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now learned how to create your own custom callback handlers.

Next, check out the other how-to guides in this section, such as [how to background callbacks](/v0.2/docs/how_to/callbacks_backgrounding).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to do retrieval with contextual compression

](/v0.2/docs/how_to/contextual_compression)[

Next

How to write a custom retriever class

](/v0.2/docs/how_to/custom_retriever)

*   [Next steps](#next-steps)