Skip to main content

Example Integration (LangChain)

LogSpend Integration for LangChain applications

You can now easily instrument your LangChain application with minimal effort using LogSpend’s LangChain integration. The integration is enabled using custom LangChain callback handlers.

All inputs and outputs to an LLM chain including intermediate steps are automatically logged. This allows for having an in-depth view of all user interactions and application actions taken to accomplish a given task for an end user or an automated process. Everything is encapsulated into a user session, so a session_id must be passed to the callback handler for every chain.

Currently, only the following actions are supported:

  • Chains: on_chain_start, on_chain_end and on_chain_error
  • LLM: on_llm_start, on_llm_end and on_llm_error
  • ChatModel: on_chat_model_start

Please reach out if you are missing relevant actions that should be tracked.

Integration Steps

  1. Install required packages
pip install -U logspend langchain
  1. Import the required modules
import os
from logspend_sdk.integrations import LogSpendLangChainCallbackHandler
  1. Import the required modules
api_key = os.environ.get('LOGSPEND_API_KEY')
if not api_key:
raise ValueError("Missing LogSpend API Key. Ensure the LOGSPEND_API_KEY environment variable is set.")
  1. Initialize the LogSpend handler with relevant metadata for each chain. Please provide a session identifier session_id which is used to encapsulate all interactions belonging to the same user session. This could be a chat ID, conversation ID, document ID or any relevant entity ID for your use case. Additionally, you can also include a user_id to represent the user (this could be an internal identifier, an email address, device ID, cookie identifier or any relevant unique ID to identify the user or visitor). Same applies to the optional ip_address field useful for enriching logs with user geographic information like city, country, and region.
logspend_handler = LogSpendLangChainCallbackHandler(
api_key=api_key,
project_id="<YOUR_LOGSPEND_PROJECT_ID>",
session_id="<CURRENT_USER_SESSION_ID>",
user_id="<CURRENT_USER_ID>",
ip_address="<CURRENT_USER_IP_ADDRESS>"
)
  1. Set any relevant custom properties on the LogSpend handler. By default, all properties passed to the chain are automatically picked up as custom properties but you can add additional custom properties that can be used to filter on the data or create custom metrics and graphs on the LogSpend dashboard. Examples of custom properties are task_name, customer_id, etc.
custom_properties_data = {
"task_name": "chatbot-qa",
"customer_id": "cus12345",
}

logspend_handler = LogSpendLangChainCallbackHandler(
...,
custom_properties=custom_properties_data
)
  1. Add LogSpend handler as callback to your chains. See examples below for invoke, run and predict methods.
chain.invoke({"input": "<task_input>"}, config={"callbacks": [logspend_handler]})
chain.run(input="<task_input>", callbacks=[logspend_handler])
conversation.predict(input="<task_input>", callbacks=[logspend_handler])