!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/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
    *   [Integrations](/v0.1/docs/ecosystem/integrations/)
        
        *   [Databerry](/v0.1/docs/ecosystem/integrations/databerry/)
        *   [Helicone](/v0.1/docs/ecosystem/integrations/helicone/)
        *   [Lunary](/v0.1/docs/ecosystem/integrations/lunary/)
        *   [Google MakerSuite](/v0.1/docs/ecosystem/integrations/makersuite/)
        *   [Unstructured](/v0.1/docs/ecosystem/integrations/unstructured/)
    *   [Integrating with LangServe](/v0.1/docs/ecosystem/langserve/)
    *   [LangSmith](https://docs.smith.langchain.com)
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Ecosystem](/v0.1/docs/ecosystem/)
*   [Integrations](/v0.1/docs/ecosystem/integrations/)
*   Lunary

On this page

Lunary
======

This page covers how to use [Lunary](https://lunary.ai?utm_source=langchain&utm_medium=js&utm_campaign=docs) with LangChain.

What is Lunary?[​](#what-is-lunary "Direct link to What is Lunary?")
--------------------------------------------------------------------

Lunary is an [open-source](https://github.com/lunary-ai/lunary) platform that provides observability (tracing, analytics, feedback tracking), prompt templates management and evaluation for AI apps.

Installation[​](#installation "Direct link to Installation")
------------------------------------------------------------

Start by installing the Lunary package in your project:

*   npm
*   Yarn
*   pnpm

    npm install lunary

    yarn add lunary

    pnpm add lunary

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

Create an account on [lunary.ai](https://lunary.ai?utm_source=langchain&utm_medium=js&utm_campaign=docs). Then, create an App and copy the associated `tracking id`.

Once you have it, set it as an environment variable in your `.env`:

    LUNARY_APP_ID="..."# Optional if you're self hosting:# LUNARY_API_URL="..."

If you prefer not to use environment variables, you can set your app ID explictly like this:

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";const handler = new LunaryHandler({  appId: "app ID",  // verbose: true,  // apiUrl: 'custom self hosting url'});

#### API Reference:

You can now use the callback handler with LLM calls, chains and agents.

Quick Start[​](#quick-start "Direct link to Quick Start")
---------------------------------------------------------

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";import { ChatOpenAI } from "@langchain/openai";const model = new ChatOpenAI({  callbacks: [new LunaryHandler()],});

#### API Reference:

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

LangChain Agent Tracing[​](#langchain-agent-tracing "Direct link to LangChain Agent Tracing")
---------------------------------------------------------------------------------------------

When tracing chains or agents, make sure to include the callback at the run level so that all sub LLM calls & chain runs are reported as well.

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";import { initializeAgentExecutorWithOptions } from "langchain/agents";import { ChatOpenAI } from "@langchain/openai";import { Calculator } from "@langchain/community/tools/calculator";const tools = [new Calculator()];const chat = new ChatOpenAI({  model: "gpt-3.5-turbo",  temperature: 0,  callbacks: [new LunaryHandler()],});const executor = await initializeAgentExecutorWithOptions(tools, chat, {  agentType: "openai-functions",});const result = await executor.run(  "What is the approximate result of 78 to the power of 5?",  {    callbacks: [new LunaryHandler()],    metadata: { agentName: "SuperCalculator" },  });

#### API Reference:

*   [initializeAgentExecutorWithOptions](https://api.js.langchain.com/functions/langchain_agents.initializeAgentExecutorWithOptions.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`

Tracking users[​](#tracking-users "Direct link to Tracking users")
------------------------------------------------------------------

You can track users by adding `userId` and `userProps` to the metadata of your calls:

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";import { initializeAgentExecutorWithOptions } from "langchain/agents";import { ChatOpenAI } from "@langchain/openai";import { Calculator } from "@langchain/community/tools/calculator";const tools = [new Calculator()];const chat = new ChatOpenAI({  model: "gpt-3.5-turbo",  temperature: 0,  callbacks: [new LunaryHandler()],});const executor = await initializeAgentExecutorWithOptions(tools, chat, {  agentType: "openai-functions",});const result = await executor.run(  "What is the approximate result of 78 to the power of 5?",  {    callbacks: [new LunaryHandler()],    metadata: {      agentName: "SuperCalculator",      userId: "user123",      userProps: {        name: "John Doe",        email: "email@example.org",      },    },  });

#### API Reference:

*   [initializeAgentExecutorWithOptions](https://api.js.langchain.com/functions/langchain_agents.initializeAgentExecutorWithOptions.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`

Tagging calls[​](#tagging-calls "Direct link to Tagging calls")
---------------------------------------------------------------

You can tag calls with `tags`:

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";import { ChatOpenAI } from "@langchain/openai";const chat = new ChatOpenAI({  model: "gpt-3.5-turbo",  temperature: 0,  callbacks: [new LunaryHandler()],});await chat.invoke("Hello", {  tags: ["greeting"],});

#### API Reference:

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

Usage with custom agents[​](#usage-with-custom-agents "Direct link to Usage with custom agents")
------------------------------------------------------------------------------------------------

You can use the callback handler combined with the `lunary` module to track custom agents that partially use LangChain:

    import { LunaryHandler } from "@langchain/community/callbacks/handlers/lunary";import { ChatOpenAI } from "@langchain/openai";import { HumanMessage, SystemMessage } from "@langchain/core/messages";import lunary from "lunary";const chat = new ChatOpenAI({  model: "gpt-4",  callbacks: [new LunaryHandler()],});async function TranslatorAgent(query: string) {  const res = await chat.invoke([    new SystemMessage(      "You are a translator agent that hides jokes in each translation."    ),    new HumanMessage(      `Translate this sentence from English to French: ${query}`    ),  ]);  return res.content;}// By wrapping the agent with wrapAgent, we automatically track all input, outputs and errors// And tools and logs will be tied to the correct agentconst translate = lunary.wrapAgent(TranslatorAgent);// You can use .identify() on wrapped methods to track usersconst res = await translate("Good morning").identify("user123");console.log(res);

#### API Reference:

*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [HumanMessage](https://api.js.langchain.com/classes/langchain_core_messages.HumanMessage.html) from `@langchain/core/messages`
*   [SystemMessage](https://api.js.langchain.com/classes/langchain_core_messages.SystemMessage.html) from `@langchain/core/messages`

Full documentation[​](#full-documentation "Direct link to Full documentation")
------------------------------------------------------------------------------

You can find the full documentation of the Lunary LangChain integration [here](https://lunary.ai/docs/langchain?utm_source=langchain&utm_medium=js&utm_campaign=docs).

Support[​](#support "Direct link to Support")
---------------------------------------------

For any question or issue with integration you can reach out to the Lunary team via [email](mailto:vince@lunary.ai) or livechat on the website.

* * *

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

[

Previous

Helicone

](/v0.1/docs/ecosystem/integrations/helicone/)[

Next

Google MakerSuite

](/v0.1/docs/ecosystem/integrations/makersuite/)

*   [What is Lunary?](#what-is-lunary)
*   [Installation](#installation)
*   [Setup](#setup)
*   [Quick Start](#quick-start)
*   [LangChain Agent Tracing](#langchain-agent-tracing)
*   [Tracking users](#tracking-users)
*   [Tagging calls](#tagging-calls)
*   [Usage with custom agents](#usage-with-custom-agents)
*   [Full documentation](#full-documentation)
*   [Support](#support)

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.