!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/)
        
        *   [Quickstart](/v0.1/docs/modules/model_io/quick_start/)
        *   [Concepts](/v0.1/docs/modules/model_io/concepts/)
        *   [Prompts](/v0.1/docs/modules/model_io/prompts/)
            
        *   [LLMs](/v0.1/docs/modules/model_io/llms/)
            
        *   [Chat Models](/v0.1/docs/modules/model_io/chat/)
            
            *   [Quick Start](/v0.1/docs/modules/model_io/chat/quick_start/)
            *   [Streaming](/v0.1/docs/modules/model_io/chat/streaming/)
            *   [Caching](/v0.1/docs/modules/model_io/chat/caching/)
            *   [Custom chat models](/v0.1/docs/modules/model_io/chat/custom_chat/)
            *   [Tracking token usage](/v0.1/docs/modules/model_io/chat/token_usage_tracking/)
            *   [Cancelling requests](/v0.1/docs/modules/model_io/chat/cancelling_requests/)
            *   [Dealing with API Errors](/v0.1/docs/modules/model_io/chat/dealing_with_api_errors/)
            *   [Dealing with rate limits](/v0.1/docs/modules/model_io/chat/dealing_with_rate_limits/)
            *   [Tool/function calling](/v0.1/docs/modules/model_io/chat/function_calling/)
            *   [Response metadata](/v0.1/docs/modules/model_io/chat/response_metadata/)
            *   [Subscribing to events](/v0.1/docs/modules/model_io/chat/subscribing_events/)
            *   [Adding a timeout](/v0.1/docs/modules/model_io/chat/timeouts/)
        *   [Model I/O](/v0.1/docs/modules/model_io/)
        *   [Output Parsers](/v0.1/docs/modules/model_io/output_parsers/)
            
    *   [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/)
        
*   [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/)
*   [Model I/O](/v0.1/docs/modules/model_io/)
*   [Chat Models](/v0.1/docs/modules/model_io/chat/)
*   Custom chat models

On this page

Custom chat models
==================

This notebook goes over how to create a custom chat model wrapper, in case you want to use your own chat model or a different wrapper than one that is directly supported in LangChain.

There are a few required things that a chat model needs to implement after extending the [`SimpleChatModel` class](https://api.js.langchain.com/classes/langchain_core_language_models_chat_models.SimpleChatModel.html):

*   A `_call` method that takes in a list of messages and call options (which includes things like `stop` sequences), and returns a string.
*   A `_llmType` method that returns a string. Used for logging purposes only.

You can also implement the following optional method:

*   A `_streamResponseChunks` method that returns an `AsyncGenerator` and yields [`ChatGenerationChunks`](https://api.js.langchain.com/classes/langchain_core_outputs.ChatGenerationChunk.html). This allows the LLM to support streaming outputs.

Let's implement a very simple custom chat model that just echoes back the first `n` characters of the input.

    import {  SimpleChatModel,  type BaseChatModelParams,} from "@langchain/core/language_models/chat_models";import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";import { AIMessageChunk, type BaseMessage } from "@langchain/core/messages";import { ChatGenerationChunk } from "@langchain/core/outputs";export interface CustomChatModelInput extends BaseChatModelParams {  n: number;}export class CustomChatModel extends SimpleChatModel {  n: number;  constructor(fields: CustomChatModelInput) {    super(fields);    this.n = fields.n;  }  _llmType() {    return "custom";  }  async _call(    messages: BaseMessage[],    options: this["ParsedCallOptions"],    runManager?: CallbackManagerForLLMRun  ): Promise<string> {    if (!messages.length) {      throw new Error("No messages provided.");    }    // Pass `runManager?.getChild()` when invoking internal runnables to enable tracing    // await subRunnable.invoke(params, runManager?.getChild());    if (typeof messages[0].content !== "string") {      throw new Error("Multimodal messages are not supported.");    }    return messages[0].content.slice(0, this.n);  }  async *_streamResponseChunks(    messages: BaseMessage[],    options: this["ParsedCallOptions"],    runManager?: CallbackManagerForLLMRun  ): AsyncGenerator<ChatGenerationChunk> {    if (!messages.length) {      throw new Error("No messages provided.");    }    if (typeof messages[0].content !== "string") {      throw new Error("Multimodal messages are not supported.");    }    // Pass `runManager?.getChild()` when invoking internal runnables to enable tracing    // await subRunnable.invoke(params, runManager?.getChild());    for (const letter of messages[0].content.slice(0, this.n)) {      yield new ChatGenerationChunk({        message: new AIMessageChunk({          content: letter,        }),        text: letter,      });      // Trigger the appropriate callback for new chunks      await runManager?.handleLLMNewToken(letter);    }  }}

We can now use this as any other chat model:

    const chatModel = new CustomChatModel({ n: 4 });await chatModel.invoke([["human", "I am an LLM"]]);

    AIMessage {  content: 'I am',  additional_kwargs: {}}

And support streaming:

    const stream = await chatModel.stream([["human", "I am an LLM"]]);for await (const chunk of stream) {  console.log(chunk);}

    AIMessageChunk {  content: 'I',  additional_kwargs: {}}AIMessageChunk {  content: ' ',  additional_kwargs: {}}AIMessageChunk {  content: 'a',  additional_kwargs: {}}AIMessageChunk {  content: 'm',  additional_kwargs: {}}

Richer outputs[​](#richer-outputs "Direct link to Richer outputs")
------------------------------------------------------------------

If you want to take advantage of LangChain's callback system for functionality like token tracking, you can extend the [`BaseChatModel`](https://api.js.langchain.com/classes/langchain_core_language_models_chat_models.BaseChatModel.html) class and implement the lower level `_generate` method. It also takes a list of `BaseMessage`s as input, but requires you to construct and return a `ChatGeneration` object that permits additional metadata. Here's an example:

    import { AIMessage, BaseMessage } from "@langchain/core/messages";import { ChatResult } from "@langchain/core/outputs";import {  BaseChatModel,  BaseChatModelCallOptions,  BaseChatModelParams,} from "@langchain/core/language_models/chat_models";import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";export interface AdvancedCustomChatModelOptions  extends BaseChatModelCallOptions {}export interface AdvancedCustomChatModelParams extends BaseChatModelParams {  n: number;}export class AdvancedCustomChatModel extends BaseChatModel<AdvancedCustomChatModelOptions> {  n: number;  static lc_name(): string {    return "AdvancedCustomChatModel";  }  constructor(fields: AdvancedCustomChatModelParams) {    super(fields);    this.n = fields.n;  }  async _generate(    messages: BaseMessage[],    options: this["ParsedCallOptions"],    runManager?: CallbackManagerForLLMRun  ): Promise<ChatResult> {    if (!messages.length) {      throw new Error("No messages provided.");    }    if (typeof messages[0].content !== "string") {      throw new Error("Multimodal messages are not supported.");    }    // Pass `runManager?.getChild()` when invoking internal runnables to enable tracing    // await subRunnable.invoke(params, runManager?.getChild());    const content = messages[0].content.slice(0, this.n);    const tokenUsage = {      usedTokens: this.n,    };    return {      generations: [{ message: new AIMessage({ content }), text: content }],      llmOutput: { tokenUsage },    };  }  _llmType(): string {    return "advanced_custom_chat_model";  }}

This will pass the additional returned information in callback events and in the \`streamEvents method:

    const chatModel = new AdvancedCustomChatModel({ n: 4 });const eventStream = await chatModel.streamEvents([["human", "I am an LLM"]], {  version: "v1",});for await (const event of eventStream) {  if (event.event === "on_llm_end") {    console.log(JSON.stringify(event, null, 2));  }}

    {  "event": "on_llm_end",  "name": "AdvancedCustomChatModel",  "run_id": "b500b98d-bee5-4805-9b92-532a491f5c70",  "tags": [],  "metadata": {},  "data": {    "output": {      "generations": [        [          {            "message": {              "lc": 1,              "type": "constructor",              "id": [                "langchain_core",                "messages",                "AIMessage"              ],              "kwargs": {                "content": "I am",                "additional_kwargs": {}              }            },            "text": "I am"          }        ]      ],      "llmOutput": {        "tokenUsage": {          "usedTokens": 4        }      }    }  }}

Tracing (advanced)[​](#tracing-advanced "Direct link to Tracing (advanced)")
----------------------------------------------------------------------------

If you are implementing a custom chat model and want to use it with a tracing service like [LangSmith](https://smith.langchain.com/), you can automatically log params used for a given invocation by implementing the `invocationParams()` method on the model.

This method is purely optional, but anything it returns will be logged as metadata for the trace.

Here's one pattern you might use:

    export interface CustomChatModelOptions extends BaseChatModelCallOptions {  // Some required or optional inner args  tools: Record<string, any>[];}export interface CustomChatModelParams extends BaseChatModelParams {  temperature: number;}export class CustomChatModel extends BaseChatModel<CustomChatModelOptions> {  temperature: number;  static lc_name(): string {    return "CustomChatModel";  }  constructor(fields: CustomChatModelParams) {    super(fields);    this.temperature = fields.temperature;  }  // Anything returned in this method will be logged as metadata in the trace.  // It is common to pass it any options used to invoke the function.  invocationParams(options?: this["ParsedCallOptions"]) {    return {      tools: options?.tools,      n: this.n,    };  }  async _generate(    messages: BaseMessage[],    options: this["ParsedCallOptions"],    runManager?: CallbackManagerForLLMRun  ): Promise<ChatResult> {    if (!messages.length) {      throw new Error("No messages provided.");    }    if (typeof messages[0].content !== "string") {      throw new Error("Multimodal messages are not supported.");    }    const additionalParams = this.invocationParams(options);    const content = await someAPIRequest(messages, additionalParams);    return {      generations: [{ message: new AIMessage({ content }), text: content }],      llmOutput: {},    };  }  _llmType(): string {    return "advanced_custom_chat_model";  }}

* * *

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

[

Previous

Caching

](/v0.1/docs/modules/model_io/chat/caching/)[

Next

Tracking token usage

](/v0.1/docs/modules/model_io/chat/token_usage_tracking/)

*   [Richer outputs](#richer-outputs)
*   [Tracing (advanced)](#tracing-advanced)

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.