Repo Optimizer:我让 KISS AI 代理在一夜之间自行优化。它将自己的成本削减了 98%。

发布: (2026年2月12日 GMT+8 23:32)
9 分钟阅读
原文: Dev.to

I’m sorry, but I don’t have access to external websites, so I can’t retrieve the article from the link you provided. If you paste the text you’d like translated here, I’ll be happy to translate it into Simplified Chinese while preserving the formatting and code blocks.

设置

I maintain KISS,一个基于单一原则构建的极简主义多代理框架:保持简单
The framework’s flagship coding agent, RelentlessCodingAgent, is a single‑agent system with smart auto‑continuation—it runs sub‑sessions of an LLM‑powered coding loop, tracks progress across sessions, and keeps hammering at a task until it succeeds or exhausts its budget. The agent was self‑evolved to run relentlessly.

它是一个单代理系统,具备智能自动续写功能——它运行由 LLM 驱动的编码循环的子会话,跟踪跨会话的进度,并持续敲击任务直至成功或耗尽预算。该代理通过自我进化实现了不间断运行。

It works, but it was expensive. A single run with Claude Sonnet 4.5 cost $3–5 and took 600–800 s. For a framework that preaches simplicity and efficiency, that felt like hypocrisy.

它能工作,但成本高昂。使用 Claude Sonnet 4.5 单次运行费用为 $3–5,耗时 600–800 秒。对于一个倡导简洁和高效的框架来说,这感觉像是虚伪。

So I built a 69‑line Python script and told it, in plain English, to fix the problem.

于是我编写了一个 69 行的 Python 脚本,并用简洁的英文让它解决这个问题。

KISS AI Agent

工具:repo_optimizer.py

整个优化器是一个指向自身源代码的 RelentlessCodingAgent。下面是脚本的核心部分:

from kiss.agents.coding_agents.relentless_coding_agent import RelentlessCodingAgent

TASK = """
Can you run 'uv run src/kiss/agents/coding_agents/relentless_coding_agent.py'
in the background so that I can see its output and you monitor the output in real time?
If you observe any repeated errors in the output, please fix them and run the command again.
Once the command succeeds, analyze the output and optimize
src/kiss/agents/coding_agents/relentless_coding_agent.py
so that it runs reliably, faster, and with less cost.
Keep repeating the process until the running time and the cost are reduced significantly,
such 99%.
...
"""

agent = RelentlessCodingAgent("RepoAgent")
result = agent.run(
    prompt_template=TASK,
    model_name="claude-opus-4-6",
    work_dir=PROJECT_ROOT,
)

就是这样。代理自行运行,监视输出,诊断问题,编辑自己的代码,然后再次运行——循环进行——直到指标下降。

没有梯度下降。没有超参数网格搜索。没有奖励模型。只有一个读取日志并重写源文件的 LLM。

实际上优化器的作用

反馈循环的工作方式如下:

  1. 运行 目标代理在基准任务上并捕获输出。
  2. 实时监控 日志。如果代理崩溃或出现重复错误,修复代码并重新运行。
  3. 分析 成功运行的情况:实际耗时、令牌数量、美元成本。
  4. 优化 源代码,使用纯英文描述的策略——压缩提示、切换模型、消除浪费的步骤。
  5. 重复,直到指标出现平台期或达到目标降幅。

这些策略本身只是任务提示中的要点:

  • 保持意义的更短系统提示
  • 删除冗余指令
  • 最小化对话轮次
  • 批量操作,使用提前终止
  • 在网络上搜索能够提升效率和可靠性的代理模式

优化器并未硬编码任何特定技术。它会阅读、推理、实验并迭代。它选择的技术取决于日志揭示的情况。

结果

经过一夜运行,优化器生成了以下报告:

指标之前 (Claude Sonnet 4.5)之后 (Gemini 2.5 Flash)降低比例
时间~600–800 秒169.5 秒~75 %
成本~$3–5$0.1296–98 %
令牌数百万级300,729大幅减少

所有三个基准测试在优化后均通过:钻石依赖解析、循环检测和故障传播。

优化器的更改

优化器自主发现并实施了九项具体修改:

  1. 模型切换 – Claude Sonnet 4.5($3/$15 每 M tokens)→ Gemini 2.5 Flash($0.30/$2.50 每 M tokens)— 输入费用降低 10 倍,输出费用降低 6 倍。
  2. 压缩提示词 – 删除冗长的 CODING_INSTRUCTIONS 样板,缩短 TASK_PROMPTCONTINUATION_PROMPT,但不失原意。
  3. 新增 Write() 工具 – 原始代理仅有 Edit(),在唯一性冲突时会失败。每次失败会浪费 2–3 步;加入 Write() 后消除了这一问题。
  4. 更强的结束指令 – “立即在测试通过后调用 finish。进行额外验证。”——阻止代理在冗余确认运行上消耗 token。
  5. Bash 超时指引 – “为测试运行设置 timeout_seconds=120”——防止并行 Bash 执行时卡死。
  6. 有界轮询循环 – “使用有界轮询循环,绝不使用无界等待”——消除后台进程的无限循环风险。
  7. 降低 max_steps – 25 → 15。迫使代理在仍能完成任务的前提下更加高效。
  8. 简化步骤阈值 – 始终使用 max_steps - 2,而非复杂的自适应计算。
  9. 移除 CODING_INSTRUCTIONS 导入 – 消除每次提示中加载的不必要 token 开销。

这些改动并不陌生,事后看都很显而易见。它们共同实现了 98 % 的成本削减。关键在于 没有任何人工介入——优化器通过实验自行发现并验证了每一项修改。

为什么这样有效

RelentlessCodingAgent 是一个通用的编码循环:它以自然语言获取任务,能够使用 Bash、ReadEditWrite 工具,并运行子会话直至成功。repo_optimizer.py 只是复用了同一个循环,只是向内部指向。

这之所以可行,是因为 KISS 框架的三个特性:

  1. 代理仅是 Python 函数。 没有配置 c

(原始内容到此结束;后续解释在来源中被截断。)

更大的图景:repo_agent.py

优化器实际上是一个更简单工具 repo_agent.py 的特化。它是一个 28 行的脚本,接受任意任务作为命令行参数,并在你的项目根目录执行:

uv run python -m kiss.agents.coding_agents.repo_agent "Add retry logic to the API client."

repo agent 和 repo optimizer 共享相同的引擎(RelentlessCodingAgent)和相同的接口(一个字符串)。唯一的区别是任务。优化器的任务恰好是 “optimize this agent for speed and cost.”(“优化此代理以提升速度和降低成本。”)它同样可以是 “add comprehensive test coverage”(“添加全面的测试覆盖率”)或 “migrate from REST to GraphQL.”(“从 REST 迁移到 GraphQL”。)

KISS 中的代理并不在乎你让它们做什么。它们在乎的是 relentlessly until it’s done(“不懈地完成任务”。)

亲自尝试

# Install KISS
# https://github.com/ksenxx/kiss_ai/README.md

# Run the repo optimizer on your own codebase
uv run python -m kiss.agents.coding_agents.repo_optimizer

# Or give the repo agent any task in plain English
uv run python -m kiss.agents.coding_agents.repo_agent "Refactor the database layer for connection pooling."

该框架、代理和优化器全部开源:
github.com/ksenxx/kiss_ai

KISS 由 Koushik Sen 构建。欢迎贡献。

0 浏览
Back to Blog

相关文章

阅读更多 »

KAIzen — AI 时代对敏捷的需求

一家游戏公司的小团队如何将流效率从 32% 提升到 85%——通过改变我们提供给 AI 的内容。我们的团队严格遵循 Scrum:两周的 s...