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

# Monitor LangChain Agents with Galileo

> Learn how to build and monitor a LangChain AI Agent using Galileo for tracing and observability

{/*<!-- markdownlint-enable MD044 -->*/}

## Overview

### What you'll build

A simple LangChain-powered AI Agent that uses OpenAI's language models and a custom tool, with all agent activities logged and monitored in Galileo.

### What you'll learn

* How to configure a LangChain Agent
* How to integrate Galileo for observability and monitoring
* How to structure tools and environment for scalable development

👀 Check out the full [SDK Examples repository on GitHub](https://github.com/rungalileo/SDK-Examples), or [watch the video walkthrough](https://youtu.be/kx0LbMKZFUg) in tandem.

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/kx0LbMKZFUg" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

### Requirements

* Python package manager + some familiarity with Python (for the sake of this cookbook, we'll use [uv](https://docs.astral.sh/uv/))
* A Galileo Developer Account. If you don't have one, [sign up for free](https://app.galileo.ai).
* OpenAI Key to assist [get one here](https://platform.openai.com/api-keys)

## Environment setup

### Ingredients

* git
* Python environment tools
* Package manager (pip or uv)

### Steps

1. **Clone the repository:**

   ```bash theme={null}
   git clone https://github.com/rungalileo/sdk-examples.git
   cd sdk-examples/python/agent/langchain-agent
   ```

2. **Create a virtual environment:**
   **on Windows**

   ```bash theme={null}
   python -m venv venv
   venv\Scripts\activate
   ```

   **on Mac**
   Using a standard virtual environment

   ```bash theme={null}
   python -m venv venv
   source venv/bin/activate
   ```

   Or using uv (faster)

   ```bash theme={null}
   uv venv venv
   source venv/bin/activate
   ```

3. **Install dependencies:**
   Using pip

   ```bash theme={null}
   pip install -r requirements.txt
   ```

   OR using uv

   ```bash theme={null}
   uv pip install -r requirements.txt
   ```

4. **Set up your environment variables:**
   Copy the existing `.env.example` file, and rename it to `.env` in your project directory.

   Set your Galileo and OpenAI environment variables:

   ```ini .env theme={null}
   # Your Galileo API key
   GALILEO_API_KEY="your-galileo-api-key"

   # Your Galileo project name
   GALILEO_PROJECT="your-galileo-project-name"

   # The name of the Log stream you want to use for logging
   GALILEO_LOG_STREAM="your-galileo-log-stream"

   # Provide the console url below if you are using a
   # custom deployment, and not using the free tier, or app.galileo.ai.
   # This will look something like “console.galileo.yourcompany.com”.
   # GALILEO_CONSOLE_URL="your-galileo-console-url"

   # OpenAI properties
   OPENAI_API_KEY="your-openai-api-key"

   # Optional. The base URL of your OpenAI deployment.
   # Leave this commented out if you are using the default OpenAI API.
   # OPENAI_BASE_URL="your-openai-base-url-here"

   # Optional. Your OpenAI organization.
   # OPENAI_ORGANIZATION="your-openai-organization-here"
   ```

   * Replace the values with your actual keys. This keeps your credentials secure and out of your code.

## Understand the agent architecture

### 🧠 Agent core (`main.py`)

A single script defines:

* Loading of secrets
* Tool declaration
* Agent instantiation
* Galileo observability

### 🛠️ Tools

Simple `@tool` functions that the agent can call, such as:

```python theme={null}
@tool
def greet(name: str) -> str:
    """Say hello to someone."""
    return f"Hello, {name}! 👋"
```

### 🔍 Instrumentation (`galileo_context` + `GalileoCallback`)

The `galileo_context` tags all logs under a project and stream.

The `GalileoCallback` automatically traces agent behavior in Galileo.

## Main agent workflow

### Key ingredients

* LangChain agent
* OpenAI model
* Galileo integration

### How it works

1. Load `.env` variables.
2. Declare tools.
3. Wrap agent execution in `galileo_context`.
4. Use `GalileoCallback` to trace the run.
5. Print the agent's response.

## Running the agent

Run your script using:

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

### Expected output

```text theme={null}
Agent Response:
Hello, Erin! 👋
```

## View traces in Galileo

1. Log into [Galileo](https://app.galileo.ai).
2. Open the `langchain-docs` project and `my_log_stream`.
3. Inspect:

   * Prompts
   * Reasoning steps
   * Tool invocations
   * Outputs

## Extending the agent

### Add new tools

Define more `@tool`-decorated functions and include them in the agent.

### Change models

Swap out `gpt-4` for another supported OpenAI model in `ChatOpenAI`.

### Update context

Change the `project` and `log_stream` in `galileo_context` for better trace organization.

## Conclusion

### Key takeaways

* LangChain + Galileo makes AI agents traceable and observable
* Using tools and context managers helps modularize and organize agent behavior
* Monitoring enables better debugging and optimization

### Next steps

* Check out the [Galileo YouTube Channel](https://www.youtube.com/@rungalileo) to see what you can build!
* Star the [Galileo SDK-examples repo](https://github.com/rungalileo/sdk-examples)to bookmark more ways to get started with the Galileo SDK.
* Follow [Galileo on LinkedIn](https://www.linkedin.com/company/galileo-ai/) to stay in touch with the latest news and resources.

Happy building! 🚀

## Common issues and solutions

### API key issues

**Problem:** "Invalid API key" errors

**Solution:**

* Double-check your `.env` file

### Galileo connection issues

**Problem:** Traces aren't showing up in Galileo

**Solution:**

* Confirm your API key is valid
* Check internet connectivity
* Ensure `flush()` is being called at the end of execution
