Skip to main content

Agent Harness Runtime

轻量级 Agent 引擎。一条命令或两行代码即可启动 LLM Agent。

ahr run "查一下 SKHY 最新股价"
from ahr import Agent
print(Agent().run("查一下 SKHY 最新股价").output)

快速开始

pip install ahr
ahr run "你好"

或配置 API key 后:

# 方式一:环境变量
export OPENAI_API_KEY=sk-...
export OPENAI_API_BASE=https://your-api.com/v1
export MODEL_NAME=your-model
ahr run "1+2=?"

# 方式二:配置文件 ~/.config/ahr/config.json
cat > ~/.config/ahr/config.json <<'EOF'
{"OPENAI_API_KEY": "sk-...", "OPENAI_API_BASE": "https://...", "MODEL_NAME": "your-model"}
EOF

# 方式三:.env 文件(优先级:shell env > .env > config.json)

外部引用 — 最简用法

from ahr import Agent, tool

# 一行
result = Agent().run("1+2=?")
print(result.output)

# 多轮对话
agent = Agent()
agent.run("我的名字是张三")
agent.run("我叫什么名字?")      # 记得
agent.close()

# 并行任务
results = Agent().batch(["task1", "task2", "task3"], workers=3)

# 持久化到文件
agent = Agent(journal=True)
agent.run("重要任务")             # 自动写入 events.jsonl + meta.json
agent.close()

# 自定义工具
@tool
def get_weather(city: str) -> str:
    """查城市天气。

    Args:
        city: 城市名
    """
    return f"{city}: 25°C"

with Agent(tools=[get_weather]) as a:
    print(a.run("北京天气").output)

三层架构

类 位置 职责 隐喻
Agent ahr.Agent public sync 入口 方向盘
Runtime runtime.Runtime async 生命周期管理 驾驶座
Engine runtime.core.agent.Engine 内部 ReAct 循环 发动机

CLI

ahr run "task"                 # 执行任务(自动持久化 events.jsonl)
ahr tui                        # 交互式 REPL
ahr ps                         # 列出所有运行
ahr show <run_id>              # 查看运行详情
ahr debug config               # 检查配置

测试

pytest                           # 108 确定性测试
pytest --smoke=only              # 真实 LLM 端到端测试
pytest tests/unit/               # 纯逻辑测试
pytest tests/integration/        # Engine + MockLLMClient

项目结构

ahr/                              CLI + Public API
├── __init__.py                   Agent, AgentResult, run, tool, 内建工具
├── api.py                        Agent sync 包装器
├── cli.py                        argparse 入口
├── cmd_run.py                    run / resume / fork
├── cmd_ps.py                     ps
├── cmd_show.py                   show
├── cmd_approval.py               approve / reject
├── cmd_stop.py                   stop
├── cmd_debug.py                  debug
└── tui.py                       交互式 REPL(rich)

runtime/                          Agent 引擎(零 ahr 反向依赖)
├── runtime.py                    Runtime async facade
├── factory.py                    build_runtime / build_agent 构建器
├── settings.py                   Settings 数据类(零 import-time 副作用)
├── context.py                    ContextManager + SlidingWindow
├── toolkit.py                    ToolExecutor + HookPipeline
├── skills.py                     技能发现
├── guardrails.py                 输入/输出护栏
├── core/
│   ├── agent.py                  Engine — 核心 ReAct 循环
│   ├── consumer.py               EventConsumer + Printer Protocol(UI 输出解耦)
│   ├── llm.py                    LLMClient(stream/invoke)
│   └── types.py                  消息类型 + @tool 装饰器 + AgentResult
├── events/
│   ├── schema.py                 5 种事件 dataclass
│   ├── protocol.py               EventSink 协议
│   ├── recorder.py               EventRecorder + reconstruct_state
│   ├── store.py                  运行目录 / meta / 用量聚合
│   └── runs.py                   run / resume / fork 会话生命周期
└── tools/
    ├── __init__.py               default_tools()(file + shell + web_search + MCP)
    ├── builtin/                  file_ops, code_exec, web_search, task
    └── mcp/                      MCP 客户端(JSON-RPC 2.0 over stdio/HTTP)

tests/
├── unit/                         纯逻辑(~51 测试)
├── integration/                  Engine + MockLLMClient(~65 测试)
└── smoke/                        真实 LLM(1 测试)

Metadata

Release files for ahr 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ahr 0.1.0
File Size Uploaded Provenance
ahr-0.1.0.tar.gz 53.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ahr 0.1.0
File Interpreter ABI Platform
ahr-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 121.4 kB

Release files / ahr-0.1.0.tar.gz

Download URL ahr-0.1.0.tar.gz
Size 53.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bb08b2e28fef771d589762768ac21d268ba6122cc2b563276dcd1efb1d20322b
BLAKE2b-256 checksum
How to use checksums
394e5bc3512f49f32e55a41e37d2dbfd812624d29ff25fa2f41a365f6e7c418f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ahr-0.1.0-py3-none-any.whl

Download URL ahr-0.1.0-py3-none-any.whl
Size 68.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45de61f25c4f4009e2dd253dd3524c07953fc11db2699cfbf40788bb0451951e
BLAKE2b-256 checksum
How to use checksums
08771c3f7671bc5c66972cfc5ec33b812bf29bf62c49053214071323d7e8c856
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for ahr 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page