> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tokenfactory.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain Integration

[LangChain](https://python.langchain.com/) is a popular framework for building AI agents.
The ConTree integration provides a `ContreeSandbox` backend that lets LangChain agents execute code in isolated, reproducible containers.

Integration is available via `contree-sdk[langchain]`, which provides `ContreeSandbox` — an implementation of [`BaseSandbox`](https://docs.langchain.com/oss/python/deepagents/backends/sandbox) from the [deepagents](https://pypi.org/project/deepagents/) package.

## Using ContreeSandbox

```python theme={null}
import asyncio
import os

from deepagents import create_deep_agent
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from pydantic import SecretStr

from contree_sdk import Contree
from contree_sdk.langchain.sandbox import ContreeSandbox


async def main():
    client = Contree()
    image = await client.images.oci("python:3.13-slim")
    session = image.session()
    sandbox = ContreeSandbox(session=session)

    model = ChatOpenAI(
        model="zai-org/GLM-5.2",
        base_url="https://api.studio.nebius.ai/v1/",
        api_key=SecretStr(os.environ["NEBIUS_API_KEY"]),
    )

    agent = create_deep_agent(model=model, backend=sandbox)
    result = await agent.ainvoke({"messages": [HumanMessage("Develop a small calculator script and run it")]})
    print(result["messages"][-1].content)


if __name__ == "__main__":
    asyncio.run(main())
```

## Setup

1. Install the dependencies:

   ```bash theme={null}
   pip install "contree-sdk[langchain]"
   ```
2. Set up Nebius IAM token and base URL:

   ```bash theme={null}
   export NEBIUS_API_KEY="your-nebius-iam-token"
   export NEBIUS_PROJECT_ID="your-project-id"
   ```
3. Set up your LLM provider credentials (e.g. for Anthropic):

   ```bash theme={null}
   export ANTHROPIC_API_KEY="your-anthropic-api-key"
   ```
