結論先說
- 實時狀態靠
response.tool_calls,計時靠hooks:response.tool_calls實時提供call.name與call.args;起止毫秒耗時與失敗次數須聯合pre_tool_call_decide、post_tool_call與on_tool_error採集。 - 拆開模型推理與工具執行時間才能定位根因:許多卡頓源自
run_command跑全量構建或stdioMCP 回傳海量日誌。 - 監控入參體積可預警上下文膨脹:單次傳入巨型載荷會抬高下輪推理耗時並加速觸發壓縮。
- 連續失敗應設熔斷提示:同一工具連續觸發
on_tool_error時,返回糾偏說明引導模型換路徑或中止。 - 多工具高頻調優推薦高配額度:頻繁對多模組開展流式剖析可參考 Gemini Google AI Ultra 5x/20x 充值指南 與 Gemini Google AI Ultra 5x 人工 iOS。

圖:本站根據 Google Antigravity SDK Tools 文檔 整理的示意圖
本文根據 Google Antigravity 官方文檔整理,資料核對於 2026-12-03。配圖為本站整理的示意圖,不是介面截圖。本站(bigolab.com)由益陽市赫山區必高科技有限公司獨立運營,是獨立第三方服務商,不是 Google 官方網站。
適用於誰
- 分不清 Agent 耗時卡在“模型思考”還是“工具/MCP 執行”的後端工程師。
- 需統計工具調用延遲、入參體積與錯誤率,量化定位瓶頸的平台架構師。
- 開發自定義 Python 工具時遇到模型重複報錯調用、需及時熔斷的開發者。
一、四類工具監控手段對比
| 監控點位 | 捕獲數據 | 耗時統計能力 | 監控用途 |
|---|---|---|---|
response.tool_calls | call.name/args | 僅知起點,缺結束時間 | 前端實時進度條 |
@hooks.pre_tool_call_decide | 入參字典 | 啟動 perf_counter() 計時 | 算入參字節並預警大載荷 |
@hooks.post_tool_call | ToolResult | 與起點做差得毫秒耗時 | 統計平均延遲與成功數 |
@hooks.on_tool_error | Exception 異常 | 記失敗耗時與錯誤數 | 算重試率並返回糾偏提示 |
把起點存入關聯列表並在 post_tool_call / on_tool_error 彈出結算,即成無侵入工具 APM 探針。

圖:本站根據 Google Antigravity SDK Hooks 文檔 整理的示意圖
二、流式監聽 tool_calls 並統計耗時示例
以下實時打印 tool_calls,並用鉤子彙總調用次數、失敗數、均耗時與最大入參體積:
import asyncio
import json
import time
from collections import defaultdict
from google.antigravity import Agent, LocalAgentConfig, hooks, types
stats = defaultdict(lambda: {"calls": 0, "errors": 0, "total_ms": 0.0, "max_bytes": 0})
inflight: list[tuple[str, float]] = []
@hooks.pre_tool_call_decide
async def before_tool(call: types.ToolCall) -> types.HookResult:
size = len(json.dumps(call.args or {}, ensure_ascii=False).encode("utf-8"))
stats[call.name]["calls"] += 1
stats[call.name]["max_bytes"] = max(stats[call.name]["max_bytes"], size)
inflight.append((call.name, time.perf_counter()))
return types.HookResult(allow=True)
@hooks.post_tool_call
async def after_tool(result: types.ToolResult) -> None:
for i in range(len(inflight) - 1, -1, -1):
if inflight[i][0] == result.name:
_, t0 = inflight.pop(i)
stats[result.name]["total_ms"] += (time.perf_counter() - t0) * 1000
break
@hooks.on_tool_error
async def failed_tool(err: Exception) -> str | None:
if inflight:
name, t0 = inflight.pop()
stats[name]["errors"] += 1
stats[name]["total_ms"] += (time.perf_counter() - t0) * 1000
return f"工具失敗({err}),請縮小範圍或核對參數後重試。"
async def main():
config = LocalAgentConfig(hooks=[before_tool, after_tool, failed_tool])
async with Agent(config) as agent:
res = await agent.chat("檢索當前目錄配置文件並彙總關鍵依賴版本。")
async for call in res.tool_calls:
print(f"[工具事件] {call.name} args={call.args}")
print("\n[最終結論]\n", await res.text())
for name, m in stats.items():
avg = m["total_ms"] / max(1, m["calls"])
print(f"- {name}: 次數={m['calls']}, 失敗={m['errors']}, 均耗時={avg:.1f}ms, 最大入參={m['max_bytes']}B")
if __name__ == "__main__":
asyncio.run(main())
人工核對要點:
- 存在並發工具調用時按工具名逆序匹配計時,防止耗時串算。
max_bytes超過數十 KB 時改為傳文件路徑或行區間,避免上下文膨脹。errors連續升高時檢查@hooks.on_tool_error提示語是否明確。
訂閱與開通提示:多模組性能調優與壓測可選 Gemini Google AI Ultra 5x 人工 iOS 或 Gemini Google AI Ultra 20x 人工 iOS,帳號見 Google 帳號與訂閱指南;獨立測試電郵可看 谷歌電郵成品號(質保 3 天首登、封號不在質保範圍,須遵守 Google 服務條款);對比多模型表現可搭 Claude Pro iOS 自助充值 與 Codex 500 積分加油包。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費,企業批量採購支持簽訂正式中文服務合同並對公結算。
常見問題
問:只消費 response.tool_calls 不調 await response.text(),會影響本輪收尾嗎?
答:建議讀完 response.tool_calls 後再調用 await response.text() 或遍歷正文流,確保整輪生成結束後再退出上下文。
問:為什麼工具執行只花了 50ms,兩次 tool_calls 之間卻停頓了好幾秒?
答:間隔主要是模型讀取上輪結果後進行下一輪推理的計算與網絡耗時,可降低思考檔位或精簡工具輸出。
問:自定義 Python 函數工具怎麼寫能減少模型傳參錯誤?
答:SDK 會解析 Type Hints 與 Docstring 生成參數 Schema,寫清參數類型、枚舉和示例能明顯降低傳參出錯率。
問:企業採購多套 AI 訂閱用於流水線壓測監控,如何開具 6% 增值稅發票與核對售後?
答:可前往 AI 訂閱與帳號服務總覽 與 Gemini Google AI Ultra 5x/20x 充值指南 挑選檔位,下單即可備註開票資料。全站以商品頁實時標價為準,標價不含稅,開具國內增值稅發票另付 6% 稅費;單獨申請自定義明細電子收據免費。本站明示質保訂閱、不質保封號,請嚴格遵守服務商條款合規使用。
相關教學
- Antigravity 2.0 調外部 MCP 工具總報超時報錯怎麼解:stdio 緩衝阻塞、異步心跳與重連設定
- Antigravity 使用時報錯執行被中斷怎麼辦:跨目錄讀寫拒絕、外網訪問攔截與舊版崩潰修復
- Antigravity 報錯 Agent terminated due to error 怎麼修:Reset onboarding、上下文過載與插件衝突
- Antigravity 2.0 混切 Gemini 4 與 Claude 模型省額度指南:長倉檢索與複雜重構分工策略
0 條評論
還沒有評論,來搶沙發~