This tutorial walks you through deploying a production-ready AI agent from scratch. By the end, you’ll have a working agent that can browse the web, execute code, manage files, and respond to user queries â deployed on a cloud server with monitoring.
What We’re Building
A Research Assistant Agent that:
- Accepts research topics via a simple web interface
- Searches the web and academic sources
- Summarizes findings into structured reports
- Saves reports as Markdown files
- Sends email notifications when research is complete
Prerequisites
- Python 3.11+
- An OpenRouter or OpenAI API key
- A VPS or cloud server (we use a $5/mo Hetzner instance)
- Basic terminal knowledge
Step 1: Project Setup
mkdir ai-agent-deploy && cd ai-agent-deploy
python -m venv venv
source venv/bin/activate
pip install langchain langchain-openapi duckduckgo-search fastapi uvicorn
Step 2: Define the Agent
from langchain_openai import ChatOpenAI
from langchain.agents import create_openai_functions_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.tools import DuckDuckGoSearchRun
# Use OpenRouter for multi-model access
llm = ChatOpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="your-openrouter-key",
model="anthropic/claude-sonnet-4"
)
# Define tools
search = DuckDuckGoSearchRun()
tools = [search]
# Define prompt
prompt = ChatPromptTemplate.from_messages([
("system", "You are a research assistant. Thoroughly research the given topic using web search. Provide structured, cited summaries."),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
])
# Create agent
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
Step 3: Add a Web Interface
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Research Agent API")
class ResearchRequest(BaseModel):
topic: str
class ResearchResponse(BaseModel):
topic: str
summary: str
sources: list[str]
@app.post("/research", response_model=ResearchResponse)
async def research(request: ResearchRequest):
result = agent_executor.invoke({"input": f"Research: {request.topic}"})
return ResearchResponse(
topic=request.topic,
summary=result["output"],
sources=[] # Parse from agent output in production
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Step 4: Deploy to Production
# On your server
git clone your-repo && cd ai-agent-deploy
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt
# Create systemd service
sudo tee /etc/systemd/system/research-agent.service << EOF
[Unit]
Description=Research Agent API
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/ai-agent-deploy
Environment=OPENROUTER_API_KEY=your-key
ExecStart=/opt/ai-agent-deploy/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable research-agent
sudo systemctl start research-agent
Step 5: Add Monitoring
# Add basic health check endpoint
@app.get("/health")
async def health():
return {"status": "ok", "agent": "research-assistant"}
# Monitor with a simple uptime check
# Add to crontab: */5 * * * * curl -sf http://localhost:8000/health || systemctl restart research-agent
Step 6: Test It
curl -X POST http://localhost:8000/research
-H "Content-Type: application/json"
-d '{"topic": "Latest developments in AI agent frameworks 2026"}'
Production Checklist
Before going live, ensure you have:
- API key management: Use environment variables, never hardcode keys
- Rate limiting: Add middleware to prevent abuse (e.g., slowapi)
- Input validation: Sanitize all user inputs before passing to LLM
- Cost controls: Set spending limits on your API provider
- Logging: Log all requests for debugging and cost tracking
- HTTPS: Use nginx + Let’s Encrypt for TLS termination
- Backups: Regular backups of generated research files
Cost Estimate
For a small team (10 research requests/day):
- Server: $5/mo (Hetzner CX11)
- API costs: ~$15/mo (OpenRouter with Claude Sonnet)
- Total: ~$20/mo for a fully functional research agent
Next Steps
To extend this agent:
- Add memory with LangGraph persistence for multi-session research
- Integrate arXiv API for academic paper search
- Add email notifications with SMTP integration
- Build a React frontend for non-technical users
- Deploy with Docker for reproducible environments
Last updated: July 2026
