Configuration and Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:LangChain utilizes a flexible configuration system primarily driven by environment variables and Pydantic models. This approach allows developers to manage API keys, secrets, and global settings securely and efficiently across different components and integrations. The framework's modular design means that core functionalities and partner integrations rely on this system for initialization and runtime behavior, promoting both ease of use and adherence to security best practices.
The configuration strategy is evident in how partner packages handle credentials and how the core library manages global states like debugging and tracing. By using environment variables, developers can avoid hardcoding sensitive information and easily switch between different configurations for development, testing, and production environments. This system is integral to the framework's goal of providing a composable and secure way to build LLM-powered applications.
Core Configuration System
The core configuration system in LangChain is centered around utility functions that retrieve values from the environment and manage global settings for the framework's behavior.
Value Retrieval
LangChain provides a helper function to flexibly retrieve configuration values. The primary mechanism is the get_from_dict_or_env function, which attempts to find a value from a dictionary first, and if not found, falls back to checking an environment variable. This pattern is widely used for initializing parameters like API keys.
The diagram below illustrates the lookup logic of the get_from_dict_or_env function.
Global Settings
Global settings control framework-level behaviors such as debugging and tracing. These are typically managed through functions that check environment variables to toggle features on or off.
| Environment Variable | Description | Managed By |
|---|---|---|
LANGCHAIN_DEBUG | If set to "true", enables debug mode for the framework. | langchain_core.globals.get_debug() |
LANGCHAIN_TRACING_V2 | If set to "true", enables v2 tracing with LangSmith. | langchain_core.globals.get_tracing_v2() |
LANGSMITH_TRACING | An alternative to LANGCHAIN_TRACING_V2 for enabling LangSmith tracing. | libs/cli/langchain_cli/package_template/README.md:19-20 |
LANGSMITH_API_KEY | API key for authenticating with LangSmith. | libs/cli/langchain_cli/package_template/README.md:21-22 |
LANGSMITH_PROJECT | Specifies the project name in LangSmith. Defaults to "default" if not set. | libs/cli/langchain_cli/package_template/README.md:23-24 |
Sources: libs/core/langchain_core/globals.py:12-41, libs/cli/langchain_cli/package_template/README.md:19-24
Partner Integration Configuration
Partner integrations, such as those for OpenAI and Exa, use Pydantic models to manage their configuration. This approach provides strong validation, type-hinting, and a clear structure for required and optional parameters, including API keys sourced from environment variables.
Pydantic Model Validation
A common pattern in partner packages is to use a @model_validator on a Pydantic model. This validator runs before the model is initialized to check for necessary credentials, typically API keys, in the environment. It ensures that the client or retriever is properly configured before it can be used.
The following sequence diagram shows the initialization process for a partner component like ExaSearchRetriever.
This diagram illustrates how the ExaSearchRetriever uses a Pydantic model validator to initialize its API client by checking for the EXA_API_KEY environment variable.
Sources: libs/partners/exa/langchain_exa/retrievers.py:72-76, libs/partners/exa/langchain_exa/_utilities.py
API Key Management
API keys are handled as SecretStr types from Pydantic to prevent them from being accidentally exposed in logs or tracebacks. The configuration models define fields for these keys, which are automatically populated from corresponding environment variables (e.g., OPENAI_API_KEY, EXA_API_KEY).
The class diagram below shows the configuration fields for the ExaSearchRetriever.
This diagram highlights the Pydantic fields used to configure the ExaSearchRetriever, including the exa_api_key which is loaded from the environment.
Sources: libs/partners/exa/langchain_exa/retrievers.py:38-70, libs/partners/openai/langchain_openai/llms/base.py (for a similar pattern)
Security Best Practices
The SECURITY.md document outlines crucial best practices for handling credentials and system access, which are directly applicable to managing environment variables.
- Limit Permissions: Grant credentials the minimum necessary permissions. For example, use read-only API keys for agents that only need to fetch data, not modify it. This mitigates the risk of data corruption or unauthorized access.
- Anticipate Potential Misuse: Assume that an LLM might use credentials in any way the permissions allow. If an API key allows deletion, it's safest to assume the LLM might delete data.
- Defense in Depth: Combine multiple security layers. For instance, use read-only permissions and sandbox the application (e.g., in a container) to restrict its access to the host system.
These principles are vital when configuring applications, especially when providing agents with access to external tools and data sources via API keys set in the environment.
Sources: SECURITY.md
Project Setup and CLI
When creating a new project using the LangChain CLI, environment variables are the standard way to configure essential services like LangSmith for tracing and monitoring. The project template's README.md instructs users to export variables like LANGSMITH_API_KEY to enable these features.
This standardized approach simplifies project setup and ensures that developers follow best practices for managing secrets from the beginning.
Sources: libs/cli/langchain_cli/package_template/README.md:19-24
Conclusion
LangChain's configuration system, built on environment variables and Pydantic models, provides a robust and secure foundation for building LLM applications. It standardizes how API keys and global settings are managed, promoting modularity and ease of use. By adhering to security best practices and providing clear setup instructions, the framework enables developers to connect to a vast ecosystem of tools and services while minimizing security risks.