LangChain Integration
Automatically track LangChain LLM calls by attaching Lucidic to your agent — even if it’s deeply nested.
🔗 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
2. Attach to Your 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()
:
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
🛠️ 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