今日已更新 84 条资讯 | 累计 37333 条内容
关于我们

Pydantic AI keeps one growing message list per run — and re-sends the whole thing every step

wartzar-bee 2026年08月22日 20:37 8 次阅读 来源:Dev.to

Pydantic AI gives you a clean, typed agent: define an Agent , hand it tools, call agent.run(...) , and it loops — model call, tool call, model call — until it produces a validated result. The typed ergonomics are great. What the quickstart doesn't spell out is what the model receives on each pass of that loop. I read the run graph ( pydantic_ai_slim/pydantic_ai/_agent_graph.py on main ) to find out. The mechanism is structural, and it's the same shape I found in the OpenAI Agents SDK and smolagents. One list, appended twice per turn Each run holds a single mutable conversation list on its state: message_history : list [ _messages . ModelMessage ] = dataclasses . field ( default_factory = list [ _messages . ModelMessage ]) On every model step the graph appends to it — first the outgoing request, then the model's response: ctx . state . message_history . append ( self . request ) ... ctx . state . message_history . append ( response ) Nothing is removed. The list only grows: request, response, request, response — with tool calls and, crucially, tool outputs riding inside those messages. The full list is re-sent every step When the graph builds the input for the next model call, it takes the entire accumulated history — a full copy: messages = ctx . state . message_history [:] ... messages [:] = _clean_message_history ( ctx . state . message_history ) That [:] is the whole conversation to date. So on step 1 the model sees your prompt; on step 2 it sees your prompt + step 1's request + step 1's response (including the tool output); on step 5 it sees all of that plus steps 2–4. The payload you pay for grows every single step, and the heaviest passengers are usually the tool outputs — the search results, file contents, and API responses you least want re-uploaded five times. Why it's quadratic, and why nothing warns you A run of n steps sends roughly 1 + 2 + 3 + … + n copies of history — O(n²) cumulative tokens in the step count. A 3-step agent is fine. A 12-step agent th

本文内容来源于互联网,版权归原作者所有
查看原文