LangChain Integration with Future AGI for Chain Tracing

Integrate LangChain with Future AGI for auto-instrumented tracing. Capture chain executions, tool calls, and LLM interactions with traceAI-langchain.

1. Installation

First install the traceAI package and necessary LangChain packages.

pip install traceAI-langchain
pip install langchain_openai
npm install @traceai/langchain @traceai/fi-core @opentelemetry/instrumentation \
  @langchain/openai @langchain/core

2. Set Environment Variables

Set up your environment variables to authenticate with both FutureAGI and OpenAI.

import os

os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"
process.env.OPENAI_API_KEY = "your-openai-api-key";
process.env.FI_API_KEY = "your-futureagi-api-key";
process.env.FI_SECRET_KEY = "your-futureagi-secret-key";

3. Initialize Trace Provider

Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines.

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.OBSERVE,
    project_name="langchain_project",
)
import { register, ProjectType } from "@traceai/fi-core";

const tracerProvider = register({
  projectType: ProjectType.OBSERVE,
  projectName: "langchain_project",
});

4. Instrument your Project

Initialize the LangChain Instrumentor to enable automatic tracing. This step ensures that all interactions with the LangChain are tracked and monitored.

Warning

LangChainInstrumentor().instrument(...) patches LangChain’s callback manager for the whole process, not just the code you call it from. Calling .instrument() again later (a second service, a background job, a vendored tool that also imports LangChain) is a no-op: the first tracer_provider you passed wins, and every LangChain call anywhere in that process is traced into that project. If your process runs more than one LangChain-based workflow and you want them in different projects, instrument each one with its own tracer_provider before any of them run, or keep unrelated LangChain code (linters, internal tools, vendored SDKs) in a separate process entirely.

from traceai_langchain import LangChainInstrumentor

LangChainInstrumentor().instrument(tracer_provider=trace_provider)
import { LangChainInstrumentation } from "@traceai/langchain";
import * as CallbackManagerModule from "langchain/callbacks";

// Pass the custom tracer provider to the instrumentation
const lcInstrumentation = new LangChainInstrumentation({
  tracerProvider: tracerProvider,
});

// Manually instrument the LangChain module
lcInstrumentation.manuallyInstrument(CallbackManagerModule);

5. Create LangChain Components

Set up your LangChain pipeline as you normally would. Our Instrumentor will automatically trace and send the telemetry data to our platform.

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template("{x} {y} {z}?").partial(x="why is", z="blue")
chain = prompt | ChatOpenAI(model_name="gpt-3.5-turbo")

result = chain.invoke({"y": "sky"})

print(f"Response: {result}")
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromTemplate("{x} {y} {z}?").partial({ x: "why is", z: "blue" });
const chain = prompt.pipe(new ChatOpenAI({ model: "gpt-3.5-turbo" }));

const result = await chain.invoke({ y: "sky" });
console.log("Response:", result);
Was this page helpful?

Questions & Discussion