🔗 LangChain Integration

Lucidic integrates seamlessly with LangChain agents and chains — no need to rewrite your code or restructure your logic.


⚙️ How It Works

When you use provider="langchain" with Lucidic, you can attach a special handler that will:

  • Recursively search your agent for LangChain LLMs
  • Automatically attach itself as a callback handler
  • Capture LLM usage as Lucidic Events, grouped under the correct Step

This means you get full observability into your agent’s reasoning — with zero boilerplate instrumentation.


🧪 Setup Instructions

1. Initialize Lucidic

import lucidicai as lai

lai.init(
    session_name="langchain_agent_run",
    agent_id="your-agent-id",
    lucidic_api_key="your-api-key",
    providers=["langchain"]
)

2. Attach to Your Agent

handler = lai.LucidicLangchainHandler()
handler.attach_to_llms(agent)

✅ That’s it — even if your LLMs are deeply nested inside custom wrappers or chains, we’ll find and attach to them automatically.


✅ What Gets Captured

For every LangChain LLM call, we’ll log:

  • Input: your prompt to the LLM (e.g. "What's the capital of France?")
  • Model: the LLM model used (e.g. gpt-4, gpt-3.5-turbo)
  • Output: the LLM response

Based on your input, we automatically calculate the following:

  • Cost: estimated based on input/output tokens

🔍 Under the Hood

Here’s a simplified view of what’s happening when you call attach_to_llms():

def attach_to_llms(self, llm_or_chain_or_agent):
    if hasattr(obj, 'callbacks'):
        obj.callbacks.append(self)
    for attr in dir(obj):
        if hasattr(attr, 'callbacks'):
            attr.callbacks.append(self)

Your agent might be deeply nested — but Lucidic recursively walks through the object and attaches itself to any LLMs it finds.

📘 You don’t need to dig into .llm_chain.llm or manually insert the handler. We do that for you.


🧪 Full Example

import lucidicai as lai
from my_agent import MyLangChainAgent

lai.init(
    session_name="langchain_test",
    agent_id="agent-123",
    providers=["langchain"]
)

agent = MyLangChainAgent()

handler = lai.LucidicLangchainHandler()
handler.attach_to_llms(agent)

agent.run("Search for Stanford University")

🛠️ Requirements

  • langchain pip installed
  • Works with BaseLanguageModel, LLMChain, AgentExecutor, and custom toolchains
  • You must call attach_to_llms() before executing the agent
  • Events are attached to the currently active Step