AGENTS / GITHUB / mamba-agents
githubinferredactive

mamba-agents

provenance:github:sequenzia/mamba-agents
WHAT THIS AGENT DOES

Mamba Agents is a framework designed to simplify the creation of AI agents. It builds upon pydantic-ai, providing a streamlined way to manage the complexities of agent development. Developers can focus on the core logic of their agents while Mamba Agents handles tasks like context window management, token tracking, and cost estimation. The framework is extensible and offers a range of built-in tools and integrations. It's particularly useful for building agents that need to handle long conversations, track costs, and integrate with various models and services. Mamba Agents aims to make building production-ready AI agents more accessible and efficient.

PROBLEM IT SOLVES

Building AI agents often involves managing complex operational details like context window limits and cost tracking, which can distract from the core agent logic. Mamba Agents solves this by providing a ready-made infrastructure that handles these complexities, allowing developers to quickly create and deploy agents without getting bogged down in low-level implementation details.

View Source ↗First seen 4mo agoNot yet hireable

CAPABILITIES & CONSTRAINTS

TECH & STACK
pythonai-agentpydanticllmcontext-managementtokenizationobservabilityollama
README
# Mamba Agents

[![PyPI version](https://img.shields.io/pypi/v/mamba-agents.svg)](https://pypi.org/project/mamba-agents/)
[![Python Version](https://img.shields.io/pypi/pyversions/mamba-agents.svg)](https://pypi.org/project/mamba-agents/)
[![CI](https://github.com/sequenzia/mamba-agents/actions/workflows/ci.yml/badge.svg)](https://github.com/sequenzia/mamba-agents/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Documentation](https://img.shields.io/badge/docs-mkdocs-blue)](https://sequenzia.github.io/mamba-agents)

A simple, extensible AI Agent framework built on [pydantic-ai](https://ai.pydantic.dev/).

Mamba Agents provides a thin wrapper around pydantic-ai that adds production-ready infrastructure for building AI agents. It handles the operational complexity—context window management, token tracking, cost estimation, and observability—so you can focus on your agent's logic.

**Why Mamba Agents?**
- **Context that scales** - Automatic compaction keeps conversations within token limits using 5 different strategies
- **Cost visibility** - Track token usage and estimate costs across all your agent interactions
- **Tool ecosystem** - Built-in filesystem, bash, glob, and grep tools with security sandboxing
- **MCP ready** - Connect to Model Context Protocol servers for extended capabilities
- **Flexible prompts** - Jinja2 templates with versioning and inheritance
- **Local model support** - Works with Ollama, vLLM, and LM Studio out of the box

## Features

- **Simple Agent Loop** - Thin wrapper around pydantic-ai with tool-calling support
- **Built-in Tools** - Filesystem, glob, grep, and bash operations with security controls
- **MCP Integration** - Connect to Model Context Protocol servers (stdio, SSE, and Streamable HTTP transports)
- **Token Management** - Track usage with tiktoken, estimate costs
- **Context Compaction** - 5 strategies to manage long conversations
- **Prompt Management** - Jinja2-based templates with versioning and inheritance
- **Workflows** - Orchestration patterns for multi-step execution (ReAct built-in, extensible for custom patterns)
- **Model Backends** - OpenAI-compatible adapter for Ollama, vLLM, LM Studio
- **Observability** - Structured logging, tracing, and OpenTelemetry hooks
- **Error Handling** - Retry logic with tenacity, circuit breaker pattern

## Architecture Overview

```
                        ┌─────────────────┐
                        │      Agent      │
                        │  (pydantic-ai)  │
                        └────────┬────────┘
                                 │
         ┌───────────────────────┼───────────────────────┐
         │                       │                       │
         ▼                       ▼                       ▼
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│     Context     │    │     Token       │    │     Prompt      │
│    Management   │    │    Tracking     │    │    Management   │
└────────┬────────┘    └─────────────────┘    └─────────────────┘
         │
         ▼
┌─────────────────┐
│   Compaction    │  (5 strategies: sliding_window, summarize_older,
│   Strategies    │   selective_pruning, importance_scoring, hybrid)
└─────────────────┘

External Integrations:
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│  MCP Servers    │    │ Model Backends  │    │  Observability  │
│(stdio/SSE/HTTP) │    │ (Ollama, vLLM)  │    │ (OTEL, logging) │
└─────────────────┘    └─────────────────┘    └─────────────────┘
```

## Table of Contents

- [Requirements](#requirements)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Agent Patterns](#agent-patterns)
- [Configuration](#configuration)
- [Built-in Tools](#built-in-tools)
- [MCP Integration](#mcp-integration)
- [Context Management](#context-management)
- [Prompt Management](#prompt-management)
- [Token Management](#token-management)
- [Workflows](#workflows)
- [Model Backends](#model-backends)
- [Error Handling](#error-handling)
- [Observability](#observability)
- [Development](#development)
- [License](#license)

## Requirements

- **Python 3.12+** required
- An API key for your model provider (OpenAI, Anthropic, etc.) or a local model server (Ollama, vLLM, LM Studio)

## Installation

Install from [PyPI](https://pypi.org/project/mamba-agents/):

```bash
# Using uv (recommended)
uv add mamba-agents

# Using pip
pip install mamba-agents
```

## Quick Start

### Synchronous Usage (Simplest)

```python
from mamba_agents import Agent, AgentSettings

# Load settings from env vars, .env, ~/mamba.env, config.toml
settings = AgentSettings()

# Create agent
agent = Agent("gpt-4o", settings=settings)

# Run a query
result = agent.run_sync("What is 2 + 2?")
print(result.output)

# Multi-turn conversation (context maintained automatically)
agent.run_sync("Remember my name is Alice")
result = agent.run_sync("What's my name?")
print(result.output)  # "Alice"

# Check usage and cost
print(f"Tokens used: {agent.get_usage().total_tokens}")
print(f"Estimated cost: ${agent.get_cost():.4f}")
```

### Async Usage

```python
import asyncio
from mamba_agents import Agent, AgentSettings

async def main():
    settings = AgentSettings()
    agent = Agent("gpt-4o", settings=settings)

    # Run async queries
    result = await agent.run("What files are in the current directory?")
    print(result.output)

    # Multi-turn conversation
    result2 = await agent.run("Can you list only the Python files?")
    print(result2.output)

    # Access context state
    state = agent.get_context_state()
    print(f"Messages: {state.message_count}, Tokens: {state.token_count}")

asyncio.run(main())
```

## Agent Patterns

The `Agent` class supports multiple initialization patterns:

### Using Settings (Recommended)

```python
from mamba_agents import Agent, AgentSettings

# Load from env vars, .env, ~/mamba.env, config.toml
settings = AgentSettings()

# Uses model, api_key, and base_url from settings.model_backend
agent = Agent(settings=settings)

# Override model but use api_key/base_url from settings
agent = Agent("gpt-4o-mini", settings=settings)
```

### Direct Model String

```python
# Requires OPENAI_API_KEY environment variable
agent = Agent("gpt-4o")
```

### With a Model Instance

```python
from pydantic_ai.models.openai import OpenAIModel

model = OpenAIModel("gpt-4o")
agent = Agent(model)
```

### With Tools

```python
from mamba_agents.tools import read_file, run_bash, grep_search

agent = Agent("gpt-4o", tools=[read_file, run_bash, grep_search], settings=settings)
```

### With MCP Toolsets

```python
from mamba_agents.mcp import MCPClientManager

# Load MCP servers from .mcp.json file
manager = MCPClientManager.from_mcp_json(".mcp.json")

# Pass toolsets to Agent (pydantic-ai manages server lifecycle)
agent = Agent("gpt-4o", toolsets=manager.as_toolsets(), settings=settings)
```

> **Security Note:** API keys are stored using Pydantic's `SecretStr` and are never logged or exposed in error messages.

## Configuration

### Environment Variables

All settings use the `MAMBA_` prefix. Variables can be set in:
- Environment variables
- `.env` file (project-specific)
- `~/mamba.env` (user-wide defaults)

```bash
# Model configuration
MAMBA_MODEL_BACKEND__MODEL=gpt-4o
MAMBA_MODEL_BACKEND__API_KEY=sk-...
MAMBA_MODEL_BACKEND__BASE_URL=https://api.openai.com/v1

# Logging
MAMBA_LOGGING__LEVEL=INFO
MAMBA_LOGGING__FORMAT=json

# Retry behavior
MAMBA_RETRY__MAX_RETRIES=3
MAMBA_RETRY__RETRY_LEVEL=2
```

### TOML Configuration

Create a `config.toml` file:

```toml
[model_backend]
model = "gpt-4o"
base_url = "https://api.openai.com/v1"

[logging]
level = "INFO"
format = "json"
redact_sensitive = true

[retry]
max_retries = 3
retry_level = 2

[context]
strategy = "hybrid"
trigger_threshold_tokens = 100000
target_tokens = 80000
```

## Built-in Tools

| Tool | Description |
|------|-------------|
| `read_file` | Read con

[truncated…]

PUBLIC HISTORY

First discoveredMar 22, 2026

IDENTITY

inferred

Identity inferred from code signals. No PROVENANCE.yml found.

Is this yours? Claim it →

METADATA

platformgithub
first seenJan 6, 2026
last updatedFeb 11, 2026
last crawled1 day ago
version

README BADGE

Add to your README:

![Provenance](https://getprovenance.dev/api/badge?id=provenance:github:sequenzia/mamba-agents)