aihomelabprivacy

Build Your First AI Agent with LangChain

🤖 Researched and drafted automatically from the official docs, and reviewed before publishing. Commands are taken from the source projects — but always sanity-check before running anything on your own hardware.

LangChain is a framework for building agents and LLM applications on your own hardware. Instead of relying on cloud platforms, you can run the entire stack locally—chaining together language models, tools, and data sources with a clean Python API. This guide walks you through setting up LangChain on a homelab machine and building a working agent.

Prerequisites

You need Python 3.9 or later and pip or uv installed. A local LLM (via Ollama or similar) or API credentials for a remote model (OpenAI, Anthropic, etc.) will work. For serious agent work, allocate at least 8GB RAM; 16GB is safer.

Step 1: Install LangChain

Use uv for fast dependency resolution:

uv add langchain

Or with pip:

pip install langchain

This installs the core framework. You’ll add integrations as needed.

Step 2: Set Up a Local Model Connection

For privacy and control, use a local LLM. Install Ollama (ollama.ai) on your homelab machine, then pull a model:

ollama pull mistral

Ollama runs on localhost:11434 by default. Alternatively, if you’re using an API key (OpenAI, etc.), set it as an environment variable:

export OPENAI_API_KEY="your-key-here"

Step 3: Create Your First Agent Script

Create a file called agent.py:

from langchain.chat_models import init_chat_model

# Initialize the model
model = init_chat_model("openai:gpt-4")

# For local Ollama, use:
# model = init_chat_model("ollama:mistral", base_url="http://localhost:11434")

# Simple invocation
result = model.invoke("What is the capital of France?")
print(result.content)

Run it:

python agent.py

You should see a response from the model.

Step 4: Add Tools to Your Agent

Agents become useful when they can call external tools. Install the tools package:

uv add langchain-community

Create a simple agent with a calculator tool:

from langchain.chat_models import init_chat_model
from langchain.tools import tool
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain import hub

@tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

tools = [add, multiply]

model = init_chat_model("ollama:mistral", base_url="http://localhost:11434")

# Get the default agent prompt from LangChain Hub
prompt = hub.pull("hwchase17/openai-tools-agent")

# Create the agent
agent = create_tool_calling_agent(model, tools, prompt)

# Execute the agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = agent_executor.invoke({
    "input": "What is 5 plus 3, then multiply by 2?"
})

print(result["output"])

Run it:

python agent.py

The agent will parse your request, call the tools, and return the result.

Step 5: Connect to Real Data Sources

For practical agents, integrate with your own data. Install the necessary packages:

uv add langchain-core langchain-text-splitters

Create an agent that can search and summarize local files:

from langchain.chat_models import init_chat_model
from langchain.tools import tool
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain import hub
import os

@tool
def search_files(query: str) -> str:
    """Search for files matching a keyword."""
    results = []
    for root, dirs, files in os.walk("/home/user/documents"):
        for file in files:
            if query.lower() in file.lower():
                results.append(os.path.join(root, file))
    return "\n".join(results) if results else "No files found."

@tool
def read_file(path: str) -> str:
    """Read the contents of a file."""
    try:
        with open(path, 'r') as f:
            return f.read()[:1000]  # First 1000 chars
    except Exception as e:
        return f"Error reading file: {e}"

tools = [search_files, read_file]

model = init_chat_model("ollama:mistral", base_url="http://localhost:11434")
prompt = hub.pull("hwchase17/openai-tools-agent")
agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = agent_executor.invoke({
    "input": "Find and summarize any files about budget in my documents."
})

print(result["output"])

Step 6: Run Behind a Reverse Proxy (Optional)

If you want to expose your agent API safely to other machines on your LAN, use a reverse proxy. This keeps the agent process internal:

uv add fastapi uvicorn

Create api.py:

from fastapi import FastAPI
from langchain.chat_models import init_chat_model

app = FastAPI()

model = init_chat_model("ollama:mistral", base_url="http://localhost:11434")

@app.post("/agent")
async def run_agent(prompt: str):
    result = model.invoke(prompt)
    return {"response": result.content}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)

Run it:

python api.py

Access only from localhost or behind a VPN tunnel. Never expose this to the public internet without authentication and rate limiting.

Step 7: Persist Agent State (Optional)

For agents that need memory across sessions, store conversation history:

from langchain.chat_models import init_chat_model
from langchain.memory import ConversationBufferMemory
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain import hub

memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

model = init_chat_model("ollama:mistral", base_url="http://localhost:11434")
prompt = hub.pull("hwchase17/openai-tools-agent")
agent = create_tool_calling_agent(model, [], prompt)

agent_executor = AgentExecutor(
    agent=agent,
    tools=[],
    memory=memory,
    verbose=True
)

result = agent_executor.invoke({"input": "Remember my name is Alice."})
result = agent_executor.invoke({"input": "What is my name?"})

print(result["output"])

Monitoring and Debugging

Enable verbose logging to see what your agent is doing:

import logging

logging.basicConfig(level=logging.DEBUG)

For production-grade observability, LangChain integrates with LangSmith (langsmith.com), which provides tracing, evals, and debugging. Set up a free account and configure:

export LANGSMITH_API_KEY="your-key"
export LANGSMITH_PROJECT="my-agent"

Then your agent runs are automatically logged and visible in the LangSmith dashboard.

Security Notes

  • Keep agents on your LAN or behind a VPN. Never expose the API directly to the internet.
  • If using API keys (OpenAI, etc.), store them in environment variables or a .env file—never commit them to version control.
  • Validate and sanitize any user input passed to agent tools, especially file paths.
  • Rate-limit API endpoints if you expose them to other machines on your network.

Is It Worth It?

Yes, if you want to run AI agents without cloud costs or data leaving your network. LangChain’s abstraction layer means you can swap models (local Ollama to OpenAI to Anthropic) without rewriting code. The framework handles the plumbing—prompt templates, tool calling, memory—so you focus on the agent logic. For a homelab, the learning curve is gentle and the payoff is real: you get a programmable AI that understands your tools and data.

Related video

New self-hosted AI & homelab shorts, daily.

Subscribe on YouTube

Related guides

← All guides