ddtrace is a Datadog application performance monitoring (APM) library which provides an integration to monitor your LangChain application.Key features of the ddtrace integration for LangChain:
- Traces: Capture LangChain requests, parameters, prompt-completions, and help visualize LangChain operations.
- Metrics: Capture LangChain request latency, errors, and token/cost usage (for OpenAI LLMs and chat models).
- Logs: Store prompt completion data for each LangChain operation.
- Dashboard: Combine metrics, logs, and trace data into a single plane to monitor LangChain requests.
- Monitors: Provide alerts in response to spikes in LangChain request latency or error rate.
Installation and Setup
- Enable APM and StatsD in your Datadog Agent, along with a Datadog API key. For example, in Docker:
- Install the Datadog APM Python library.
- The LangChain integration can be enabled automatically when you prefix your LangChain Python application command with
ddtrace-run:
DD_AGENT_HOST, DD_TRACE_AGENT_PORT, or DD_DOGSTATSD_PORT.
Additionally, the LangChain integration can be enabled programmatically by adding patch_all() or patch(langchain=True) before the first import of langchain in your application.
Note that using ddtrace-run or patch_all() will also enable the requests and aiohttp integrations which trace HTTP requests to LLM providers, as well as the openai integration which traces requests to the OpenAI library.
Configuration
See the APM Python library documentation for all the available configuration options.Log Prompt & Completion Sampling
To enable log prompt and completion sampling, set theDD_LANGCHAIN_LOGS_ENABLED=1 environment variable. By default, 10% of traced requests will emit logs containing the prompts and completions.
To adjust the log sample rate, see the APM library documentation.
Note: Logs submission requires DD_API_KEY to be specified when running ddtrace-run.
Troubleshooting
Need help? Create an issue on ddtrace or contact Datadog support.Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.