Agentic React ·
我们如何把 agent 定位 React 源码所需的 token 减半
Agent 改 UI 通常从一张截图开始,然后花 token 看像素、搜仓库。React 本来就知道每个元素由哪个组件渲染、组件在哪个文件里。我们的思路是把这些信息直接交给 agent。用 Agentic React,你在浏览器里选中元素,复制它的组件、稳定的 selector、源文件和行号、owner trace 和附近代码,agent 从正确的那一行开始。

核心结论
在三个源码定位任务里,Agentic React 上下文用了 180,302 个 token,截图用了 388,694 个。token 少 53.6%,运行时间少 21.8%,API 等价成本低 66.1%,并且每次都找到了正确的源码。
痛点:截图只告诉 agent 长什么样,不告诉它在哪
截图展示的是页面长什么样。它不说明是哪个组件渲染的,也不说明组件在哪个文件里。Agent 先花 token 描述图片,再花更多 token 在仓库里搜它看到的文字,而且还需要一个能看图的模型。
Agentic React 怎么工作
Agentic React 有一个与 bundler 无关的 core,以及 Vite、Webpack、Next.js 的 adapter。本地开发时,你选中一个或多个元素,Agentic React 为每个元素复制一段文本或 JSON。Dev server 还在 /mcp 上把同样的上下文作为本地 MCP tool 提供给 agent。上下文是纯文本,任何模型都能用。
调节面板把新的文字颜色、字体这类选择写成明确的指令,每条指令都放在 agent 应该修改的文件和行号旁边。
一个元素复制出来的上下文
<web_context type="react_component_location">
component: ProfileField
tuning prompts:
- Change <ProfileField> in src/components/UserProfile/ProfileField.jsx:19 text color to rgb(15, 118, 110).
selector: #profile-field-email
source: src/components/UserProfile/ProfileField.jsx:19
source trace:
-> <ProfileField> at src/components/UserProfile/ProfileField.jsx:19
-> <ProfileContent> at src/components/UserProfile/ProfileContent.jsx:54
</web_context>Vite 配置
pnpm install @agentic-react/vite -D
// vite.config.ts
import { defineConfig } from 'vite'
import AgenticReact from '@agentic-react/vite'
export default defineConfig({ plugins: [AgenticReact()] })Benchmark
我们让 coding agent 做同一个只读任务 6 次:找到渲染指定 UI 目标的最小源码区域,返回紧凑的 JSON。三个 UI 区域各跑两次,一次附裁剪截图,一次粘贴 Agentic React 上下文。Agent 是 codex-cli 0.143.0 里的 gpt-5.4(reasoning effort 为 low),记录日期是 2026-07-28。
成本按 API 等价价格计算:未缓存输入每百万 token $2.50,缓存输入每百万 token $0.25,输出每百万 token $15.00。原始清单和完整方法都已公开。
| UI 区域 | 输入 | 输入 token | 缓存输入 | 输出 | 总计 | token 节省 | 成本 | 成本节省 |
|---|---|---|---|---|---|---|---|---|
| 问题列表行 | 截图 | 138,501 | 96,256 | 830 | 139,331 | 基线 | $0.1421265 | 基线 |
| 问题列表行 | Agentic React 上下文 | 56,429 | 38,016 | 560 | 56,989 | 59.0981% | $0.0639365 | 55.0144% |
| 问题详情 | 截图 | 128,166 | 96,128 | 970 | 129,136 | 基线 | $0.1186770 | 基线 |
| 问题详情 | Agentic React 上下文 | 50,852 | 48,768 | 521 | 51,373 | 60.2179% | $0.0252170 | 78.7516% |
| 实时分析 | 截图 | 119,162 | 91,008 | 1,065 | 120,227 | 基线 | $0.1091120 | 基线 |
| 实时分析 | Agentic React 上下文 | 71,174 | 68,096 | 766 | 71,940 | 40.1632% | $0.0362090 | 66.8148% |
| 合计 | 截图 | 385,829 | 283,392 | 2,865 | 388,694 | 基线 | $0.3699155 | 基线 |
| 合计 | Agentic React 上下文 | 178,455 | 154,880 | 1,847 | 180,302 | 53.6134% | $0.1253625 | 66.1105% |
现在做到哪一步
这些包发布在 npm 的 @agentic-react 命名空间下:core、vite、webpack、next。vertile.ai 自己的开发就在用 @agentic-react/next。
我们用 8 个固定 commit 的开源 React 应用搭了一个更大的 benchmark,覆盖主流 bundler 和 React 框架:Vite 加 TanStack Router、webpack 5、Rsbuild、Turbopack 和 webpack 上的 Next.js、TanStack Start、带 React islands 的 Astro,以及 React Router v7 加 Express。任务跑在 Harbor 上。
Agentic React 和 agentic-node 分工合作。agentic-node 折叠 Node 打印的内容:构建、dev server 和测试。Agentic React 负责浏览器呈现的内容:UI 本身、hydration 错误和 console 输出。两者合起来,覆盖了 agent 开发 React 应用时读到的输出。
接下来
下一步,Agentic React 会把 hydration 错误、console 刷屏这类浏览器输出压缩成 agent 需要的几行,并放在引发它们的组件和源码行旁边。
然后我们在这 8 个应用的 benchmark 上,分别在有和没有 Agentic React 的情况下跑 agent,公布任务成功率、output token 和轮数。
把 @agentic-react/vite、@agentic-react/webpack 或 @agentic-react/next 加进 dev dependencies,选中一个元素,把它的上下文贴给你的 agent。