三个工具,让 agent 在一次对话里完成研究、写码、调试与保存

简介: 本文展示了一个真正“具身智能”的多工具Agent:它能自动研究网页、编写/调试Python代码(如列表推导式)、运行验证并保存结果。核心不在工具本身,而在模型自主规划工作流——研究→写→测→修→存,全程无需硬编码逻辑。三工具即成系统,智能涌现于规划。

agent 抓了一份 Python 文档,写了三段 list comprehension 示例,然后跑起来。前两段没问题第三段抛出了语法错误。它没有停在那里,而是去读错误信息、找到问题、把代码改了,再跑一次。这次过了,到这一刻"agentic" 这个词才真正落地。

单个工具是噱头,工具集才是真正的系统。其实只要有三个工具就能把 agent 从聊天机器人变成能干活的东西:

fetch_url

读取任意网页,

write_file

把结果落到磁盘,

run_python

直接执行代码。一次对话里,研究、写、测、修可以全部走完。

真正有意思的不是工具本身而是规划,让agent 自己决定:"得先研究,再写代码,最后运行验证。" 这个顺序不是写死在代码里的;模型从目标推理出来,自动成形。

下面这段代码是一个完整的多工具 agent,能做网页研究、写并运行 Python,也能保存文件。

 import anthropic  
import subprocess, sys, tempfile, re  
import urllib.request  
from pathlib import Path  
client = anthropic.Anthropic()  
OUTPUT_DIR = Path("agent_output")  
OUTPUT_DIR.mkdir(exist_ok=True)  
# ── Tool implementations ──────────────────────────────────────────────────  
def fetch_url(url: str) -> str:  
    """Fetch a webpage and strip HTML - gives the agent access to the web."""  
    try:  
        req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})  
        with urllib.request.urlopen(req, timeout=10) as r:  
            html = r.read().decode("utf-8", errors="ignore")  
        text = re.sub(r"<[^>]+>", " ", html)  
        text = re.sub(r"\s+", " ", text).strip()  
        return text[:2500]  # cap to avoid filling the context window  
    except Exception as e:  
        return f"Error fetching {url}: {e}"  
def write_file(filename: str, content: str) -> str:  
    """Save content to the output directory."""  
    path = OUTPUT_DIR / filename  
    path.write_text(content)  
    return f"Saved {len(content)} chars to {path}"  
def run_python(code: str) -> str:  
    """Execute Python code in a subprocess. Returns stdout and stderr."""  
    with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:  
        f.write(code)  
        tmp = f.name  
    try:  
        result = subprocess.run(  
            [sys.executable, tmp],  
            capture_output=True, text=True, timeout=15  
        )  
        out = result.stdout or ""  
        err = result.stderr or ""  
        return (out + ("\nERROR:\n" + err if err else "")).strip() or "Ran with no output."  
    except subprocess.TimeoutExpired:  
        return "Error: timed out after 15 seconds."  
    finally:  
        Path(tmp).unlink(missing_ok=True)  
TOOLS = [  
    {  
        "name": "fetch_url",  
        "description": "Fetch the text content of any webpage. Use for research.",  
        "input_schema": {"type":"object","properties":{"url":{"type":"string"}},"required":["url"]}  
    },  
    {  
        "name": "write_file",  
        "description": "Save text to a file in the output directory.",  
        "input_schema": {"type":"object","properties":{"filename":{"type":"string"},"content":{"type":"string"}},"required":["filename","content"]}  
    },  
    {  
        "name": "run_python",  
        "description": "Execute Python code and return the output. Use to test code you write.",  
        "input_schema": {"type":"object","properties":{"code":{"type":"string"}},"required":["code"]}  
    }  
]  
def execute_tool(name, inputs):  
    if name == "fetch_url":  return fetch_url(inputs["url"])  
    if name == "write_file": return write_file(inputs["filename"], inputs["content"])  
    if name == "run_python": return run_python(inputs["code"])  
    return f"Unknown tool: {name}"  
# ── Agent loop ─────────────────────────────────────────────────────────────  
def run_agent(task: str):  
    print(f"\nTask: {task}\n" + "="*50)  
    messages = [{"role": "user", "content": task}]  
    system = """You are a research and coding agent. When given a task:  
1. Research first if needed (fetch_url)  
2. Write any code required  
3. Run the code to verify it works - fix it if it errors  
4. Save the final result to a file  
Be methodical. Show your reasoning before each tool call."""  
    for i in range(15):  
        response = client.messages.create(  
            model="claude-haiku-4-5-20251001",  
            max_tokens=1024,  
            system=system,  
            tools=TOOLS,  
            messages=messages  
        )  
        if response.stop_reason == "end_turn":  
            answer = next((b.text for b in response.content if hasattr(b,"text")), "Done.")  
            print(f"\nDone: {answer}")  
            return answer  
        tool_results = []  
        for block in response.content:  
            if block.type == "tool_use":  
                print(f"\n  [{block.name}]")  
                result = execute_tool(block.name, block.input)  
                # Print a preview so you can follow along  
                print(f"  → {result[:200]}{'...' if len(result)>200 else ''}")  
                tool_results.append({  
                    "type": "tool_result",  
                    "tool_use_id": block.id,  
                    "content": result  
                })  
        messages.append({"role":"assistant","content":response.content})  
        messages.append({"role":"user","content":tool_results})  
# Run it  
run_agent(  
    "Write three Python list comprehension examples - basic, filtered, and nested. "  
    "Run each one to verify it works. Save a cheat sheet to 'list_comprehensions.md'."  
 )
fetch_url

用正则把 HTML 标签去掉,方法粗糙,但拿到可读文本是够用的;timeout 用来防止 agent 在慢站上卡住。

run_python

走的是临时文件加 subprocess 隔离,比

exec()

安全一些,但放到生产环境仍有风险,timeout 同样用来防死循环。

system prompt 给 agent 划了一条工作路径:研究、写代码、测试、保存。它会自然按这个节奏走。

多工具带来的真正变化

agent 本身不会写代码,也不会上网。它只会用工具。智能落在规划上。

丢一个复杂任务进去,看它的执行序列:抓 URL → 综合信息 → 写代码 → 运行 → 读错误 → 改 bug → 再跑 → 保存结果。这条工作流不是你编排出来的,是模型自己推出来的。

这就是为什么工具设计比挑模型更关键。一个设计得当的工具,能让 agent 的能力上一个台阶。

最值得学习的一点:agent 会调试自己写的代码。看到报错,理解原因,动手修。这不是预设脚本,是循环加工具自然涌现出来的行为。

改进提高

加一个

read_file

工具,从输出目录读取已有文件。然后把这个任务交给 agent:"读取你之前生成的 list comprehensions cheat sheet,再加两个进阶示例,把文件更新一遍。"

看它怎么走:读 → 分析 → 扩展 → 保存。同一套规划逻辑,落到完全不同的工作流上。

https://avoid.overfit.cn/post/061983d61b58422e8436c0eece200573

by TechnoFin

目录
相关文章
|
11天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23460 10
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
15天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
4991 17
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
16天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
5979 14
|
5天前
|
人工智能 缓存 Shell
Claude Code 全攻略:命令大全 + 实战工作流(完整版)
Claude Code 是一款运行在终端环境下的 AI 编码助手,能够直接在项目目录中理解代码结构、编辑文件、执行命令、执行开发计划,并支持持久化记忆、上下文压缩、后台任务、多模型切换等专业能力。对于日常开发、项目维护、快速重构、代码审查等场景,它可以大幅减少手动操作、提升编码效率。本文从常用命令、界面模式、核心指令、记忆机制、图片处理、进阶工作流等维度完整说明,帮助开发者快速上手并稳定使用。
989 1
|
4天前
|
前端开发 API 内存技术
对比claude code等编程cli工具与deepseek v4的适配情况
DeepSeek V4发布后,多家编程工具因未适配其强制要求的`reasoning_content`字段而报错。本文对比Claude Code、GitHub Copilot、Langcli、OpenCode及DeepSeek-TUI等主流工具的兼容性:Claude Code需按官方方式配置;Langcli表现最佳,开箱即用且无报错;Copilot与OpenCode暂未修复问题;DeepSeek-TUI尚处早期阶段。
869 2
对比claude code等编程cli工具与deepseek v4的适配情况
|
1月前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
25334 65
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)