alpineagents
A Python agent framework where the agent loop is a function you write.
Agentholds the settings: the model, the system prompt and the tools.Stateholds one task: the messages so far and the answer.- A loop takes both and repeats one turn until a stop condition is true.
Why
In most frameworks the loop lives inside the library. LangGraph has you declare it as a graph. The OpenAI Agents SDK and PydanticAI run it for you.
In alpineagents the loop is a few lines of Python in your own code:
until=decides when it stops.limit=sets the maximum number of turns.- Compaction runs only where you call it.
The default loop, alpineagents.default_loop, is written the same way. Read it, or copy it as a starting point.
Misuse raises an error that says how to fix it.
Install
Requires Python 3.11 or later.
pip install alpineagents # or: uv add alpineagents
Anthropic models read ANTHROPIC_API_KEY. OpenAI-compatible servers read OPENAI_API_KEY.
Quick start
Define a tool, create an Agent, run a task:
from pathlib import Path
from alpineagents import Agent, tool
@tool
def read_file(path: str) -> str:
"""Read a text file"""
file = Path(path)
return file.read_text() if file.exists() else f"No such file: {path}"
agent = Agent(model="claude-sonnet-5", tools=[read_file])
print(agent.run("Summarize README.md in three lines"))
@toolturns the function into a tool. The type hints and the docstring tell the model how to call it.agent.runrepeats turns until the model answers, then returns the answer.- Progress is printed to the terminal while it runs.
Without an API key
FakeModel returns prepared replies in order. It needs no API key and no network. Replace the print line above with:
from alpineagents.testing import FakeModel, tool_call
fake = FakeModel([
tool_call("read_file", path="README.md"),
"README.md describes a Python agent framework.",
])
print(agent.copy(model=fake).run("Summarize README.md"))
The first reply asks for read_file. The Agent runs the tool, and the second reply is the answer.
Write your own loop
The Agent above uses default_loop. To change what happens in a turn, write the loop yourself:
from pathlib import Path
from alpineagents import Agent, State, compact_if_full, loop, tool
@tool
def list_files(folder: str = ".") -> list[str]:
"""List the files in a folder"""
return sorted(p.name for p in Path(folder).iterdir())
@tool
def read_file(path: str) -> str:
"""Read a file"""
file = Path(path)
return file.read_text() if file.exists() else f"No such file: {path}"
@tool
def write_file(path: str, content: str) -> None:
"""Create a file, or replace its content"""
Path(path).write_text(content)
@loop(until=State.is_answered, limit=30)
def coding(agent: Agent, state: State):
compact_if_full(agent, state)
agent.think(state)
if state.wants_tools():
agent.use_tools(state)
agent = Agent(
model="claude-sonnet-5",
system="You are a coding assistant. Read a file before you change it.",
tools=[list_files, read_file, write_file],
loop=coding,
)
print(agent.run("Add a test for the add() function in calc.py"))
codingis one turn: summarize the context if it is more than 60% full, ask the model, run the tools it asked for.@looprepeats the turn. It stops whenState.is_answeredis true, or after 30 turns.- To change the agent, add, remove or reorder lines in
coding.
Documentation
tgoddessana.github.io/alpineagents. Read it in this order:
- Concepts: how a run works, then Agent, State, loops and tools. About 15 minutes.
- Guides: one task per page, for example asking before a tool runs or testing an agent.
- API reference: every class and method.
Roadmap
- OpenAI adapter, LiteLLM adapter
- Subagents (
tools=[researcher]). Passing an Agent intools=raisesNotImplementedErrorfor now - Skills (
skills=). Passingskills=raisesNotImplementedErrorfor now agent.run_tool,agent.load_skillstate.save()/State.load()- Multimodal tool results (
Image,File) alpineagents[prices](cost calculation with genai-prices; for nowusage.costis filled only when you passprice=Price(...))alpineagents addCLI (copies the default loop and block sources into your project)- Per-adapter server-side compaction optimizations
Development
uv venv && uv pip install -e ".[dev]"
.venv/bin/python -m pytest -q
License
MIT. See LICENSE.
Release files for alpineagents 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| alpineagents-0.1.0.tar.gz | 275.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| alpineagents-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 376.9 kB
Release files / alpineagents-0.1.0.tar.gz
| Download URL | alpineagents-0.1.0.tar.gz |
|---|---|
| Size | 275.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f59d1d5a84070872d2204ef6a0b5beddeeda742608e7e4e8f3b74831fb8f9332
|
|
BLAKE2b-256 checksum How to use checksums |
cb17b01857039e3449515e89684e146f2b4e655225bd1cf07acc40b38dff3ba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / alpineagents-0.1.0-py3-none-any.whl
| Download URL | alpineagents-0.1.0-py3-none-any.whl |
|---|---|
| Size | 101.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
089f2fd3b81a98100d86a375551f676e62d89b57624281df5c3677ef5c75e956
|
|
BLAKE2b-256 checksum How to use checksums |
50a2dbe56f326c2e9868a79da80d9181119d0ffa783c6fde30ddfb521ab75fb5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|