Baia Agent #
A simple LangGraph-based CLI agent with terminal access and memory.
Features #
- LangGraph Agent - Stateful agent with tool execution
- Terminal Access - Execute shell commands safely in container
- Full Conversation History - Complete session context retained across all turns
- Smart Context Management:
- Configurable token limit (default: 225k tokens via
MAX_CONTEXT_TOKENS) - Automatic summarization at 75% threshold to extend session duration
- Summaries preserve key facts while reducing token usage by ~80-90%
- Configurable token limit (default: 225k tokens via
- Two-Tier Memory System:
- Short-term:
memory.md(10k char limit, LRU eviction, LLM deduplication) - Long-term: Persistent chat logs (
data/chats/, never expires)
- Short-term:
- OpenAI-Compatible - Works with OpenAI, Ollama, or any compatible endpoint
- Web UI Support - Built-in API server for LibreChat, Open WebUI, and other OpenAI-compatible frontends
Quick Start #
Install #
git clone https://github.com/user/baia.git
cd baia
Configure #
Copy the example file and edit:
cp .env.example .env
Edit .env:
# For Ollama (local)
OPENAI_BASE_URL=http://host.docker.internal:11434/v1
MODEL=qwen3.5:27b
OPENAI_API_KEY=ollama
# For OpenAI
OPENAI_BASE_URL=https://api.openai.com/v1
OPENAI_API_KEY=sk-...
MODEL=gpt-4o
# Context window management (optional)
MAX_CONTEXT_TOKENS=225000
Run #
CLI Mode (Default) #
# Start the container
docker compose up -d
# Interactive mode
docker exec -it baia-agent python -m baia.cli
# Single message
docker exec baia-agent python -m baia.cli --message "What's 2+2?"
# With custom model/endpoint
docker exec baia-agent python -m baia.cli --model gpt-4o --base-url https://api.openai.com/v1 --api-key sk-...
API Server Mode (for LibreChat, Open WebUI, etc.) #
Add to your .env:
API_SERVER_ENABLED=true
API_SERVER_KEY=your-secret-key-here
Then start the container. The agent will be available at http://localhost:8000/v1 as an OpenAI-compatible endpoint. See LibreChat integration for setup instructions.
Conversation History #
Every session maintains full conversation context, allowing the agent to:
- Reference previous questions and answers in the same session
- Remember multi-step instructions across multiple turns
- Understand "yes", "no", or follow-up references (e.g., "What about Blacky?" after discussing Blacky)
Context Management #
When a session reaches 75% of the configured token limit (default: 168,750 tokens):
- Agent pauses and displays a summary notification
- Last 10 messages remain intact for immediate context
- Older messages are summarized by LLM into a compact narrative (~80-90% size reduction)
- Summary injected as system message, original conversation preserved in JSON metadata
- Session continues seamlessly with reclaimed token capacity
This allows single sessions to last hours or 500+ exchanges before hitting the hard limit.
Session Persistence #
- Each session saved to
data/chats/<timestamp>.jsonon exit - Includes all messages plus any summaries generated during session
- Summaries stored separately in
context_infometadata (never overwrites original logs)
API Server #
When API_SERVER_ENABLED=true, Baia runs as an OpenAI-compatible API server instead of CLI mode.
Endpoints:
GET /health- Health check (no auth required)POST /v1/chat/completions- Chat completion
Authentication: Bearer token via Authorization: Bearer <API_SERVER_KEY> header
Example request:
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_SERVER_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"baia-agent","messages":[{"role":"user","content":"Hello"}]}'
Example response:
{
"id": "chatcmpl-baia-20261126221454-8c98be36",
"object": "chat.completion",
"created": 1795731294,
"model": "baia-agent",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! 👋 \n\nHow can I help you today?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 33,
"total_tokens": 133
}
}
The API server shares the same memory.md and tool capabilities as the CLI mode.
Tools #
The agent has seven built-in tools:
| Tool | Description |
|---|---|
execute_command |
Run shell commands |
read_memory |
Read all short-term memories from memory.md |
write_memory |
Save information to short-term memory (50k char limit, LRU eviction) |
list_memories |
List all memory entries with indices for update/delete operations |
update_memory |
Update specific memory entry by index |
delete_memory |
Delete specific memory entry by index |
search_chat_logs |
Search persistent chat history for long-term recall |
MCP Tools (Model Context Protocol) #
Baia supports dynamic tool extensions via the Model Context Protocol (MCP). MCP tools are configured in data/mcp_servers.json and loaded on agent startup.
Default MCP Servers #
Two servers come pre-configured:
- Workspace Files - Read/write files in
/tmp/baiadirectory - Web Fetcher - Fetch and read web pages
Both are enabled by default and automatically available to the agent.
Managing MCP Servers #
List All Configured Servers #
# Basic list with status
docker exec -it baia-agent python -m baia.mcp_cli list-servers
# List with available tools from each server
docker exec -it baia-agent python -m baia.mcp_cli list-servers --tools
Add a New MCP Server #
Stdio transport (local processes like npx):
docker exec -it baia-agent python -m baia.mcp_cli add-server \
--name "CoinGecko Crypto" \
--transport http \
--url "https://mcp.api.coingecko.com/mcp"
HTTP transport (remote endpoints):
docker exec -it baia-agent python -m baia.mcp_cli add-server \
--name "GitHub MCP" \
--transport stdio \
--command "npx" \
--args "-y,@modelcontextprotocol/server-github" \
--env-vars "GITHUB_PERSONAL_ACCESS_TOKEN"
Enable/Disable Servers #
# Disable a server (keeps config, just toggles off)
docker exec -it baia-agent python -m baia.mcp_cli disable-server --id <server-id>
# Re-enable a server
docker exec -it baia-agent python -m baia.mcp_cli enable-server --id <server-id>
Edit Server Configuration #
docker exec -it baia-agent python -m baia.mcp_cli edit-server \
--id <server-id> \
--args "/new/path,/another/path"
Delete a Server #
# Removes server config permanently (with confirmation prompt)
docker exec -it baia-agent python -m baia.mcp_cli delete-server --id <server-id>
View Server Details #
docker exec -it baia-agent python -m baia.mcp_cli show --id <server-id>
Common MCP Servers to Try #
| Server | Command | Description |
|---|---|---|
| Filesystem | npx -y @modelcontextprotocol/server-filesystem /path |
Read/write local files |
| Fetch | npx -y @modelcontextprotocol/server-fetch |
Fetch web pages |
| CoinGecko | HTTP: https://mcp.api.coingecko.com/mcp |
Crypto prices & market data (no auth required) |
| Memory | npx -y @modelcontextprotocol/server-memory |
Key-value store |
Agent-Native MCP Management #
The agent can also manage MCP tools via natural language:
User: "What MCP tools are available?"
Agent: Lists all configured servers and their tools
User: "Add a CoinGecko MCP server"
Agent: Prompts for details and adds it via add_mcp_server()
User: "Disable the filesystem tool"
Agent: Calls toggle_mcp_server(enable=False)
Important Notes #
-
Restart Required: After adding, enabling, or disabling MCP servers, restart the container for changes to take effect:
docker restart baia-agent -
Credentials: If an MCP server requires authentication, add credentials to
.envand reference them via--env-varswhen adding the server. -
Configuration File: All MCP server configurations are stored in
data/mcp_servers.json.
Memory System #
Short-term Memory (data/memory.md):
- Max 50,000 characters with automatic LRU eviction
- Individual entries max 500 characters each
- Entire file loaded into context window via system prompt
Long-term Memory (data/chats/):
- All conversations saved as timestamped JSON files
- Never expires or cleaned up
- Searchable via
search_chat_logstool for historical context
License #
MIT