maple-agent-tracing-langchain
Maple agent tracing: LangChain & LangGraph (Python and TypeScript)
Goal: every conversation = one Maple Agent Session. Each invoke()/stream() = one turn (one trace) with a readable transcript, chat model spans with tokens, tool spans with names/results, failed tools marked failed, sub-agents in their own lanes.
Mechanism: openinference-instrumentation-langchain (scope openinference.instrumentation.langchain). Maple groups sessions by the run's thread_id and reads transcript, tokens and tool calls from its spans. TraceConfig(enable_genai_semconv=True) is recommended: it also emits standard GenAI attributes (gen_ai.conversation.id from run metadata session_id > conversation_id > thread_id, gen_ai.input/output.messages in {role, parts} form).
TypeScript / JavaScript (LangChain.js, LangGraph.js): the JS instrumentor has no GenAI dual-write, so the setup adds a small span processor. Do Step 1 below for the key and region, then follow references/typescript.md instead of Steps 2-7. A repo with both Python and TS agents gets both setups.
Step 0: Detect
- Language:
package.jsondepending onlangchain,@langchain/coreor@langchain/langgraph→ TypeScript, see references/typescript.md (after Step 1). Python files importinglangchain/langgraph→ continue here. - Versions:
python -c "import langchain, langgraph, langchain_core; print(langchain.__version__, langchain_core.__version__)"andpip show langgraph(or readpyproject.toml/uv.lock/requirements*.txt). Tested: langchain 1.4.2, langgraph 1.2.12, langchain-core 1.6.5, langchain-openai 1.6.6, Python 3.12. Python must be >= 3.10. - Existing OTel setup. Search for
TracerProvider(,set_tracer_provider,logfire.configure,sentry_sdk.init,opentelemetry-instrument,Traceloop.init,LangChainInstrumentor,OpenAIInstrumentor,LANGSMITH_OTEL_ENABLED,LANGSMITH_TRACING_MODE.- A
TracerProviderexists → add the processors from Step 2 to it; do NOT create a second provider. LANGSMITH_OTEL_ENABLED/LANGSMITH_TRACING_MODE=otelset → it duplicates every run. Ask the user; remove it for Maple (plainLANGSMITH_TRACING=trueto LangSmith cloud is fine to keep).- OpenAI/Anthropic OpenInference instrumentors or OpenLLMetry LangChain instrumentor → duplicate model spans. Ask before removing if they serve something else.
- A
- Find: every
create_agent(,create_react_agent(,StateGraph(/.compile(, every.invoke(/.ainvoke(/.stream(/.astream(/Command(resume=call on an agent/graph/chain, where the app's chat/thread id lives, everyChatOpenAI((notebase_url) andinit_chat_model(/"openai:..."model string, and every tool that invokes another agent. - LangGraph Server (
langgraph.jsonpresent):import tracingat the top of the module(s)langgraph.json'sgraphspoints to (the dir holdingtracing.pymust be independencies);OTEL_*env goes in the server env file/container. Server threads already carryconfigurable.thread_id: one Maple session per thread, no code.