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

# LangChain

> Integrate Snow Leopard with LangChain agents for live and accurate data retrieval.

This guide shows you how to add Snow Leopard data retrieval to a [LangChain](https://python.langchain.com/) agent.

## Prerequisites

* Python 3.10+
* OpenAI API key (or another [supported model provider](https://python.langchain.com/docs/integrations/chat/))
* [Snow Leopard Cloud API key](https://docs.snowleopard.ai/cloud/getting-started.md#api-keys)
* A [Snow Leopard Cloud](https://docs.snowleopard.ai/cloud/getting-started.md) instance with at least one [data source](https://docs.snowleopard.ai/cloud/getting-started.md#adding-a-data-source) connected
* Your instance ID, from the instance's [Connection Info](https://docs.snowleopard.ai/cloud/getting-started.md#connection-info) tab

> [!NOTE]
>
> Don't have data? Load the [sample Northwind dataset](https://github.com/SnowLeopard-AI/northwind_psql) into a PostgreSQL database that Snow Leopard Cloud can reach, such as a hosted [Neon](https://neon.com) or [Supabase](https://supabase.com) database. Run the dataset's `northwind.sql` script against your database, then [add the database as a data source](https://docs.snowleopard.ai/cloud/getting-started.md#adding-a-data-source) in your instance.

## Start from scratch

Clone and run the [complete working example](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/quickstart/langchain):

```bash theme={null}
git clone https://github.com/SnowLeopard-AI/snowy-examples.git
cd snowy-examples/quickstart/langchain
```

Install dependencies:

```bash theme={null}
pip install langchain langchain-classic langchain-openai snowleopard python-dotenv
```

Set your environment variables:

```bash theme={null}
export OPENAI_API_KEY=your-openai-key
export SNOWLEOPARD_API_KEY=your-snowleopard-key
export SNOWLEOPARD_INSTANCE_ID=your-instance-id
```

Run the example:

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

## Bring your own agent

Already have a LangChain agent? Add Snow Leopard data retrieval with these steps.

### 1. Install dependencies

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

### 2. Create the Snow Leopard tool

Create a LangChain `Tool` that calls Snow Leopard to retrieve data:

```python theme={null}
from langchain_core.tools import Tool
from snowleopard import SnowLeopardClient

client = SnowLeopardClient(api_key="{your-snowleopard-api-key}")

def query_data(natural_language_query: str) -> str:
    """Query your database using natural language."""
    response = client.retrieve(
        instance_id="{your-instance-id}",
        user_query=natural_language_query
    )
    return str(response)

snowleopard_tool = Tool(
    name="query_database",
    func=query_data,
    description="Query your database with natural language questions. "
                "Describe your data here - this becomes part of the agent's context."
)
```

### 3. Add the tool to your agent

Add the Snow Leopard tool to your agent's tool list:

```python theme={null}
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o")
tools = [snowleopard_tool]  # Add to your existing tools

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant that can query databases."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
```

## Next steps

* View the [full example on GitHub](https://github.com/SnowLeopard-AI/snowy-examples/tree/main/quickstart/langchain)
* See our full [API documentation](https://docs.snowleopard.ai/index.md) to learn about the [Retrieve](https://docs.snowleopard.ai/cloud/endpoints/retrieve.md) and [Response](https://docs.snowleopard.ai/cloud/endpoints/response.md) endpoints
* Improve accuracy by giving Snow Leopard [feedback](https://docs.snowleopard.ai/cloud/endpoints/feedback.md) about your business logic and terminology
* Explore [Recipes](https://docs.snowleopard.ai/recipes) for production-ready agent examples


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.