agent_iterator

An iterator that runs an agent executor one step at a time instead of returning only the final result. An iterator lets code receive each action, tool result, and final answer as it happens.

In plain words
What is it for?
Use it to stream agent actions, inspect intermediate results, include run metadata, or handle synchronous and asynchronous execution step by step.
Why use it?
It makes the agent's normally hidden work observable and lets code react between steps. This is useful when a program needs progress updates or custom checks during execution.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/xt765/langchain-chinese-comment/agent_iterator
Clone the repo
git clone --depth 1 https://github.com/xt765/LangChain-Chinese-Comment
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 674 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00674
Opus 5 $0.00000 $0.00337
Sonnet 5 $0.00000 $0.00135
Haiku 4.5 $0.00000 $0.00067

Measured 2d ago against content hash 9934367b1226, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

agent_iterator scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

code_comment/libs/langchain/langchain_classic/agents/agent_iterator.md · 53 lines

What it actually says

libs\langchain\langchain_classic\agents\agent_iterator.py

此文档提供了 libs\langchain\langchain_classic\agents\agent_iterator.py 文件的详细中文注释。该模块提供了 AgentExecutorIterator 类,允许以迭代器的方式运行代理执行器。

功能描述

AgentExecutorIterator 是对 AgentExecutor 运行逻辑的包装,它将原本黑盒式的 runinvoke 过程转变为可观察、可干预的迭代过程。

这对于需要实时展示代理思考过程(思考 -> 行动 -> 观察)或在步骤间插入自定义逻辑的场景非常有用。

核心类:AgentExecutorIterator

代理执行器的迭代器实现,支持同步和异步迭代。

1. 初始化参数表

参数 类型 描述
agent_executor AgentExecutor 要迭代运行的代理执行器实例。
inputs Any 传递给代理的原始输入。
callbacks Callbacks 用于监控迭代过程的回调管理器。
include_run_info bool 是否在产出结果中包含运行元数据。
yield_actions bool 是否在生成动作(Action)时立即产出,而不等待工具执行结果。

2. 核心状态管理

  • intermediate_steps: 记录当前的动作-观察链条。
  • iterations: 累计迭代步数。
  • time_elapsed: 累计执行时长。

3. 主要方法

  • __next__() / __anext__(): 执行代理的下一个逻辑步。如果代理结束,则抛出 StopIteration 或返回最终结果。
  • reset(): 重置迭代器状态,准备开始新的任务运行。

内部工作流程

  1. 初始化: 预处理输入,初始化回调环境和计时器。
  2. 步进逻辑 (_iter_next_step):
    • 验证停止条件(最大次数/超时)。
    • 调用代理的 plan 方法。
    • 处理 AgentAction: 如果 yield_actions 为真,则直接返回动作;否则执行工具并记录观察结果。
    • 处理 AgentFinish: 触发结束回调并返回最终输出。
  3. 清理: 迭代结束后,触发 on_chain_end

应用场景

  • 实时日志展示: 在终端或 Web 端逐条流式输出代理的思考轨迹。
  • 断点调试: 允许开发者在代理执行特定工具前进行人工干预。
  • 自定义限制: 在迭代过程中根据动态条件(如预算、资源消耗)随时中断代理运行。
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 53 lines · 0 tokens per session scan A 9934367b1226

Subscribe to this mod's changes

agent_iterator is an agent published in the GitHub repository xt765/LangChain-Chinese-Comment (20 stars, last pushed 1mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 674 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.