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

# LangChain Integration

> Learn how to build an AI agent using Cycls and LangChain.

[LangChain](https://www.langchain.com/) is a powerful framework for developing applications powered by language models. It simplifies the process of chaining together different components like LLMs, prompts, and memory. This guide shows you how to combine LangChain's flexibility with Cycls' easy deployment to create robust AI agents.

## Prerequisites

* Python 3.9+
* `cycls` package installed
* Docker installed (for local testing)
* OpenAI API key

> **Note**: This guide uses OpenAI, but LangChain and Cycls support many providers including Anthropic, Google Gemini, Mistral, Cohere, and more. Simply swap the pip dependency and model name.

```bash theme={null}
pip install cycls
```

## Step 1: Create the Agent

Create a new file called `app.py`:

```python theme={null}
import cycls

@cycls.app(pip=["langchain", "langchain-openai"], copy=[".env"])
async def app(context):
    from langchain.chat_models import init_chat_model

    # Initialize the chat model (uses OPENAI_API_KEY from .env)
    model = init_chat_model("gpt-4o")

    # Get the user's message
    query = context.messages[-1]["content"]

    # Stream the response
    async for chunk in model.astream(query):
        if chunk.content:
            yield chunk.content

app.local()
```

## Step 2: Configure Environment

Create a `.env` file in the same directory to store your API key:

```env theme={null}
OPENAI_API_KEY=sk-proj-...
```

## Step 3: Run the Agent

Execute your agent:

```bash theme={null}
python app.py
```

The agent will start and provide an endpoint for interaction.

## Full Code

Here is the complete `app.py` file:

```python theme={null}
import cycls

@cycls.app(pip=["langchain", "langchain-openai"], copy=[".env"])
async def app(context):
    from langchain.chat_models import init_chat_model

    # Initialize the chat model (uses OPENAI_API_KEY from .env)
    model = init_chat_model("gpt-4o")

    # Get the latest user message
    query = context.messages[-1]["content"]

    # Stream the response
    async for chunk in model.astream(query):
        if chunk.content:
            yield chunk.content

app.local()
```

## Using Other LLM Providers

Swap the dependency and model name to use a different provider:

| Provider  | Pip Package              | Model Example                |
| :-------- | :----------------------- | :--------------------------- |
| OpenAI    | `langchain-openai`       | `gpt-4o`                     |
| Anthropic | `langchain-anthropic`    | `claude-sonnet-4-5-20250929` |
| Google    | `langchain-google-genai` | `gemini-3.0-pro`             |
| Mistral   | `langchain-mistralai`    | `mistral-large-latest`       |

## Deploy to Cloud

To deploy to production, set your Cycls API key and call `app.deploy()`:

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

cycls.api_key = os.getenv("CYCLS_API_KEY")

@cycls.app(pip=["langchain", "langchain-openai"], copy=[".env"])
async def app(context):
    from langchain.chat_models import init_chat_model

    model = init_chat_model("gpt-4o")
    query = context.messages[-1]["content"]

    async for chunk in model.astream(query):
        if chunk.content:
            yield chunk.content

app.deploy()
```
