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.
Trace a LangChain prompt and model call in Future AGI using the Python or TypeScript traceAI instrumentor.
1. Install
Install traceAI and the OpenAI adapter used in this example.
pip install traceAI-langchain
pip install langchain_openainpm install @traceai/langchain @traceai/fi-core @opentelemetry/instrumentation \
@langchain/openai@0.3.16 @langchain/core@^0.3.0 2. Set environment variables
Export credentials before running either example. Set OPENAI_MODEL to a chat model available to your account.
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
export OPENAI_MODEL="YOUR_MODEL_ID"
export FI_API_KEY="YOUR_FI_API_KEY"
export FI_SECRET_KEY="YOUR_FI_SECRET_KEY"
Prompts, messages, responses and tool payloads can be captured by default. To hide input and output content, set these TraceConfig controls before initialization:
export FI_HIDE_INPUTS=true FI_HIDE_OUTPUTS=true
export FI_HIDE_INPUT_MESSAGES=true FI_HIDE_OUTPUT_MESSAGES=true
Model metadata and custom attributes can still be exported. Error text may contain request data or credentials; these switches are not a guarantee that every error or custom attribute is redacted. Review the controls supported by your installed core version before tracing sensitive workloads.
3. Initialize tracing
Use the project name you want to open in Observe.
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 LangChain
Initialize instrumentation before invoking your chain.
Warning
In Python, instrumentation applies to the whole process. Repeated calls to .instrument() do not change the first tracer_provider. To trace workflows to different projects, run them in separate processes and initialize each with its own provider.
from traceai_langchain import LangChainInstrumentor
LangChainInstrumentor().instrument(tracer_provider=trace_provider)import { LangChainInstrumentation } from "@traceai/langchain";
import * as CallbackManagerModule from "@langchain/core/callbacks/manager";
// Pass the custom tracer provider to the instrumentation
const lcInstrumentation = new LangChainInstrumentation({
tracerProvider: tracerProvider,
});
// Manually instrument the LangChain module
lcInstrumentation.manuallyInstrument(CallbackManagerModule); 5. Run a chain
Keep these snippets in the same file as the setup above. Shutdown flushes pending spans before this short script exits.
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
try:
prompt = ChatPromptTemplate.from_template("{x} {y} {z}?").partial(x="why is", z="blue")
chain = prompt | ChatOpenAI(model=os.environ["OPENAI_MODEL"])
result = chain.invoke({"y": "sky"})
print(f"Response: {result}")
finally:
trace_provider.shutdown()import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
try {
const model = process.env.OPENAI_MODEL;
if (!model) throw new Error("Set OPENAI_MODEL");
const prompt = await ChatPromptTemplate.fromTemplate("{x} {y} {z}?").partial({ x: "why is", z: "blue" });
const chain = prompt.pipe(new ChatOpenAI({ model }));
const result = await chain.invoke({ y: "sky" });
console.log("Response:", result);
} finally {
await tracerProvider.shutdown();
} Open langchain_project in Future AGI to inspect the chain and model spans.
Questions & Discussion