Skip to main content

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 Splunk Agent Observability.

What you’ll learn

  • How to configure a LangChain Agent
  • How to integrate Splunk Agent Observability for observability and monitoring
  • How to structure tools and environment for scalable development
👀 Check out the full SDK Examples repository on GitHub.

Requirements

  • Python package manager + some familiarity with Python (for the sake of this cookbook, we’ll use uv)
  • A Splunk Agent Observability Developer Account. If you don’t have one, sign up for free.
  • OpenAI Key to assist get one here

Environment setup

Ingredients

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

Steps

  1. Clone the repository:
  2. Create a virtual environment: on Windows
    on Mac Using a standard virtual environment
    Or using uv (faster)
  3. Install dependencies: Using pip
    OR using uv
  4. Set up your environment variables: Copy the existing .env.example file, and rename it to .env in your project directory. Set your Splunk Agent Observability and OpenAI environment variables:
    .env
    • 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
  • Splunk Agent Observability

🛠️ Tools

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

🔍 Instrumentation (splunk_ao_context + SplunkAOCallback)

The splunk_ao_context tags all logs under a project and stream. The SplunkAOCallback automatically traces agent behavior in Splunk Agent Observability.

Main agent workflow

Key ingredients

  • LangChain agent
  • OpenAI model
  • Splunk Agent Observability integration

How it works

  1. Load .env variables.
  2. Declare tools.
  3. Wrap agent execution in splunk_ao_context.
  4. Use SplunkAOCallback to trace the run.
  5. Print the agent’s response.

Running the agent

Run your script using:

Expected output

View traces in Splunk Agent Observability

  1. Log into Splunk Agent Observability.
  2. Open the langchain-docs project and my_agent_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 agent_stream in splunk_ao_context for better trace organization.

Conclusion

Key takeaways

  • LangChain + Splunk Agent Observability 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

Star the Splunk Agent Observability SDK-examples repoto bookmark more ways to get started with the Splunk Agent Observability SDK. Happy building! 🚀

Common issues and solutions

API key issues

Problem: “Invalid API key” errors Solution:
  • Double-check your .env file

Splunk Agent Observability connection issues

Problem: Traces aren’t showing up in Splunk Agent Observability Solution:
  • Confirm your API key is valid
  • Check internet connectivity
  • Ensure flush() is being called at the end of execution