73 / 90

Day 73:ツール使用 Agent の制御ループ - モデルリクエスト・アクション・観測

リクエスト→アクション→観測のサイクルを説明し、境界付き Agent のトレースを予測し、実際の決定論的 Learning Project で観測可能な振る舞いを検証します。

エージェント 480 分(8時間)
01 120 分

モデルリクエストとアクション・観測の制御ループ

Agent支援エンジニアリングチェック

前提条件

Day 70-72 のモデル評価の概念と、Agent ツールトラックで導入された境界付き Agent Learning Project(projects/agent_learning)。

学習目標

ツール使用 Agent の制御ループの 5 つの段階 - モデルリクエスト、アクション提案、ツールディスパッチ、構造化観測、ループ継続または終了 - を説明し、各段階を決定論的 projects.agent_learning ハーネスに対応付けます。

手順

参照資料: この活動に割り当てられた既存の読解・練習時間内で、Hugging Face Agents Course: Introduction to Agents の Unit 1: What is an Agent?; What are Tools?; agent workflow; Summary; Quiz を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。1. 制御ループ:各ステップでランタイムはタスク・会話履歴・利用可能なツールスキーマをモデルへ送信し、モデルは 1 つのアクション候補を返します。ランタイムはそれを検証して境界付きツールへディスパッチし、ツールの構造化観測を履歴へ追加し、終了条件が発火するまでループを繰り返します。
2. ReAct(Yao 他 2022)は実環境で推論トレースとアクションを交互に挟みます。本 Learning Project は意図的に action と observation のイベントだけを保持し、thought や chain_of_thought フィールドは一切記録しません。収集する証拠は隠れた推論ではなく観測可能な振る舞いです。
3. ハーネスの状態遷移図:
IDLE -> MODEL_REQUEST -> ACTION_PROPOSED -> TOOL_DISPATCH -> OBSERVATION ->(MODEL_REQUEST へ戻る)... -> TERMINATE
run_task の終了理由:goal_reached(write_report の観測が ok)、tool_failure(失敗した観測が max_retries を超過)、step_budget(ステップ数が先に max_steps へ到達)。
4. 境界付きプロジェクトでは:DeterministicModelAdapter.next_action(history) がモデルの役割を演じ、_tool_call(action, workspace) が検証と実行を行い、run_task がループと予算を管理し、evaluate() が事後にワークスペース成果物を独立して再採点します。
5. 正直な適用範囲:このフィクスチャはループ構造とエラー語彙を示すもので、JSON-RPC フレーミング・能力ネゴシエーション・ストリーミングは実装していません。これは教育用モデルであり、プロトコル準拠クライアントではありません。

演習と予測記録

実行する前に、フィクスチャ summarize-notes の予測アクション列(ツール名と引数)、最終 status・steps・retries・終了理由を書きなさい。

独立習得基準

5 つのループ段階すべて、3 つの終了理由とその発火条件を独立に述べ、アクション・観測の証拠が隠れた思考連鎖を含まない理由を説明できること。

公式ソースリソースと権利証跡

linking Apache-2.0

Hugging Face Agents Course: Introduction to Agents

著者・提供機関: Hugging Face agents-course contributors | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Unit 1: What is an Agent?; What are Tools?; agent workflow; Summary; Quiz

検証バージョンと日付: Public edition checked 2026-11-01; moving pages may change

選定理由:

A sequenced introduction, examples and quizzes before formal ReAct/tool API references. Adopt the control-loop concepts, not framework-specific execution or private-reasoning logs.

ソース前提知識:

Python, language-model messages and JSON; local agent exercises use only action/observation traces.

安全フォールバック(オフライン回復性):

Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.

比較対象候補リソースと選定評価 (2)
ReAct ↗ compared_alternative [Open Access / CC BY 4.0]

Retained as the method-definition paper; not a self-contained beginner exercise course.

Anthropic tool use docs ↗ compared_alternative [Anthropic PBC copyright (no open license)]

Retained for one protocol example; vendor message formats are not interchangeable.

linking Open Access / CC BY 4.0

ReAct: Synergizing Reasoning and Acting in Language Models

著者・提供機関: Shunyu Yao, Jeffrey Zhao, Dian Yu, Nan Du, Izhak Shafran, Karthik Narasimhan, Yuan Cao | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3 & 4 (ReAct Formulation and Synergy)

検証バージョンと日付: ICLR 2023 / arXiv:2210.03629v2 (Verified: 2026-10-30)

選定理由:

Foundational academic paper introducing the interleaved Thought-Action-Observation control loop in LLM agents, establishing formal interleaving of task reasoning and external tool execution.

ソース前提知識:

Prompt engineering, token generation basics, and conversational interaction loops.

安全フォールバック(オフライン回復性):

Complete algorithmic step traces, state loop diagrams, and control loop pseudocode are self-contained in curriculum materials.

比較対象候補リソースと選定評価 (2)

Pure reasoning without environmental actions or external tool grounding; ReAct extends reasoning into an interactive environment action-observation loop.

Early neuro-symbolic modular router concept; ReAct offers simpler, prompting-native action-observation loop dynamics suitable for standard API models.

linking arXiv.org perpetual non-exclusive license / Open Access

Toolformer: Language Models Can Teach Themselves to Use Tools

著者・提供機関: Timo Schick, Jane Dwivedi-Yu, Roberto Dessì, Roberta Raileanu, Maria Lomeli, Luke Zettlemoyer, Nicola Cancedda, Thomas Scialom | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2 (Approach: Tool Use and Filtering)

検証バージョンと日付: NeurIPS 2023 / arXiv:2302.04761v1 (Verified: 2026-10-30)

選定理由:

Original paper demonstrating self-supervised discovery and execution of tool API calls (calculator, QA, search, calendar) via inline tokenized tool invocations.

ソース前提知識:

Transformer autoregressive generation, API calling syntax, and self-supervised filtering.

安全フォールバック(オフライン回復性):

Inline tool invocation formats and self-contained mock execution traces are provided in curriculum notes.

比較対象候補リソースと選定評価 (2)
Gorilla: Large Language Model Connected with Massive APIs (Patil et al. 2023) ↗ compared_alternative [Apache-2.0 / arXiv Open Access]

Focuses on massive API retrieval and documentation formatting; Toolformer focuses on the fundamental token-level seam between model generation and tool execution.

TALM: Tool Augmented Language Models (Parisi et al. 2022) ↗ compared_alternative [arXiv Open Access]

Iterative search-and-execute augmentation; Toolformer provides clearer self-supervised API call tokenization syntax.

02 120 分

summarize-notes フィクスチャのトレース予測

独立習得チェック(AI 支援なし)

前提条件

Day 73 Activity 1:5 段階の制御ループと 3 つの終了理由。

学習目標

ハーネスを実行する前に、完全なイベントトレース(すべてのアクション名・引数オブジェクト・観測 ok フラグ)を文章化した検証可能な予測を作成します。

手順

1. projects/agent_learning/README.md と fixtures.py を読みます:create_fixture は notes/alpha.txt と notes/beta.txt を作成し、expected_report() は両方の内容の連結です。
2. next_action のポリシーから、正確なツール順序と引数を導きます。
3. prediction.json に 4 つのイベントを {"action": {"name": ..., "arguments": {...}}, "observation": {"ok": true, ...}} の形で書き、予測 RunResult フィールドも添えます。
4. 観測ペイロードのキーも予測します:list_notes は files(ソート済み名)、read_note は filename と content、write_report は path を返します。
5. 不確かな点(例:ファイルがソートされるか)は、実際の実行で確認する未解決の問いとして明示します。

演習と予測記録

4 つのトレースイベントと予測 RunResult フィールド(status、steps、retries、termination)を含む prediction.json を書きなさい。Activity 3 の実実行後に自己採点します。

独立習得基準

実際の実行を見る前に、アクション名と引数が実測と一致する独立した予測を作成できたこと。外れた点はログへ正直に記録する。

公式ソースリソースと権利証跡

linking Open Access / CC BY 4.0

ReAct: Synergizing Reasoning and Acting in Language Models

著者・提供機関: Shunyu Yao, Jeffrey Zhao, Dian Yu, Nan Du, Izhak Shafran, Karthik Narasimhan, Yuan Cao | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3 & 4 (ReAct Formulation and Synergy)

検証バージョンと日付: ICLR 2023 / arXiv:2210.03629v2 (Verified: 2026-10-30)

選定理由:

Foundational academic paper introducing the interleaved Thought-Action-Observation control loop in LLM agents, establishing formal interleaving of task reasoning and external tool execution.

ソース前提知識:

Prompt engineering, token generation basics, and conversational interaction loops.

安全フォールバック(オフライン回復性):

Complete algorithmic step traces, state loop diagrams, and control loop pseudocode are self-contained in curriculum materials.

比較対象候補リソースと選定評価 (2)

Pure reasoning without environmental actions or external tool grounding; ReAct extends reasoning into an interactive environment action-observation loop.

Early neuro-symbolic modular router concept; ReAct offers simpler, prompting-native action-observation loop dynamics suitable for standard API models.

linking IETF Trust License / Open Standard

RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format

著者・提供機関: Tim Bray (Ed.) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 1-4 (JSON Syntax and Grammar)

検証バージョンと日付: IETF Standards Track RFC 8259 (Verified: 2026-10-30)

選定理由:

Foundational Internet standard defining strict JSON grammar, token parsing, and whitespace rules underpinning model tool calling serialization.

ソース前提知識:

Text encoding, UTF-8 parsing, and serialized data exchange.

安全フォールバック(オフライン回復性):

JSON grammar parsing rules and error recovery guidelines are documented in curriculum notes.

比較対象候補リソースと選定評価 (2)
RFC 7049: Concise Binary Object Representation (CBOR) ↗ compared_alternative [IETF Open Standard]

Binary serialization standard; text-based LLMs operate primarily on human-readable UTF-8 JSON interchange formats.

YAML 1.2 Specification (OASIS / yaml.org) ↗ compared_alternative [Open Standard]

Indentation-sensitive text format; JSON has universally standardized parsing across language runtimes and tool schemas.

03 120 分

境界付き Agent を実行しアクション・観測イベントを検査

Agent支援エンジニアリングチェック

前提条件

Day 73 Activity 2:この実行前に書面のトレース予測が存在すること。

学習目標

公開 CLI を実行して JSON 成果物を取得し、予測と観測のトレースを差分比較し、成果物が action/observation イベントのみを記録することを確認します。

手順

1. リポジトリルートから実行:
python -m projects.agent_learning.cli evaluate --fixture summarize-notes --output /tmp/agent-run.json
2. result.trace を検査:各イベントは action と observation のキーのみを持ち、observation.ok フラグがリトライ計数を駆動します。
3. evidence ブロックを検査:model_adapter は "deterministic-v1"(ライブモデルではなくフィクスチャ)、network は "disabled"、trace_policy は "actions and observations only" です。
4. prediction.json と成果物を差分比較し、すべての不一致と根本原因を記録します。
5. 同じコマンドを再実行し、2 つの成果物がバイト単位で一致することを確認します。決定論的振る舞いがあるからこそ証拠を検査できます。

演習と予測記録

CLI を 2 回実行して両成果物を保存し、(a) 2 つが一致すること、(b) thought や chain_of_thought キーがどこにも現れないこと、(c) report.metrics で 4 つの採点次元すべてが合格することを検証しなさい。

独立習得基準

バイト一致の再実行、思考連鎖キーのゼロ件スキャン、文書化された予測差分を検証できたこと。Agent はコマンド実行を手伝ってよいが、差分分析は自分で行う。

公式ソースリソースと権利証跡

linking Anthropic PBC copyright (no open license stated) / linked reference Linking only; no documentation text is copied

Anthropic Tool Use (Function Calling) Implementation Guide

著者・提供機関: Anthropic PBC | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Tool Use Overview, Schema Definitions, and Stop Reasons

検証バージョンと日付: Claude API Specification (Verified: 2026-10-30)

選定理由:

Detailed production reference for client-side tool definitions, tool_use content blocks, stop reason tool_use, and tool_result response formatting.

ソース前提知識:

REST API concepts, JSON schema specification, and basic Python client programming.

安全フォールバック(オフライン回復性):

Offline mock request/response cycles and tool payload schemas are provided in the repo.

比較対象候補リソースと選定評価 (2)
Google Gemini Function Calling Documentation ↗ compared_alternative [Creative Commons Attribution 4.0]

Protobuf and REST function declaration schema; Anthropic documentation provides explicit structured message block mechanics (tool_use / tool_result).

Cohere Tool Use and Multi-Step Agents Guide ↗ compared_alternative [Open Documentation]

Multi-step tool calling parameter definitions; Anthropic specification demonstrates clear separated content blocks essential for inspectable event traces.

linking OpenAI copyright (no open license stated) / linked reference Linking only; no documentation text is copied

OpenAI Function Calling and Structured Outputs Guide

著者・提供機関: OpenAI | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Function Calling Architecture and Strict Outputs

検証バージョンと日付: OpenAI API Reference (Verified: 2026-10-30)

選定理由:

Widely adopted specification establishing tool choice parameters, JSON schema function descriptions, tool_calls arrays, and role: "tool" responses.

ソース前提知識:

JSON data structures, chat completion roles, and schema validation.

安全フォールバック(オフライン回復性):

Standard schema dictionaries and simulated tool call responses are bundled in the repository.

比較対象候補リソースと選定評価 (2)
Mistral AI Function Calling Guide ↗ compared_alternative [Open Documentation]

Follows OpenAI-compatible schema convention; OpenAI documentation serves as the primary originating reference for tools and tool_calls schemas.

Ollama Tools and Function Calling Documentation ↗ compared_alternative [MIT / Open Documentation]

Local model runtime implementation of function calling; OpenAI docs document the standard schema contract and strict output validation.

04 120 分

評価:制御ループ状態機械と終了条件の監査

独立習得チェック(AI 支援なし)

前提条件

Day 73 Activity 1-3 を完了し、保存済みトレース成果物があること。

学習目標

制御ループの状態機械を独立に再構成し、すべての終了理由と正確な発火条件を列挙し、独立習得チェックのルーブリックを満たします。

手順

1. 記憶から状態遷移図を描きます:IDLE -> MODEL_REQUEST -> ACTION_PROPOSED -> TOOL_DISPATCH -> OBSERVATION -> ループまたは TERMINATE。
2. 終了表を埋めます:goal_reached = write_report の観測が ok。tool_failure = 失敗した観測の累計が max_retries(既定 2)を超過。step_budget = 先にステップ数が max_steps(既定 6)へ到達。
3. 独立ルーブリック:3 つの理由と発火条件がすべて正確、5 段階中 4 つ以上を命名、無援助。
4. 援助付きルーブリック(Agent 協同エンジニアリングチェック):Agent に run_task の行を探させてよいが、カウンタは自分でソースと保存済み成果物に照らして検証すること。
5. evaluate() がアダプタの自己申告ではなくワークスペース成果物を読み直す理由を 2 文で説明します。

演習と予測記録

agent.py を開かずに終了表と状態遷移リストを完成させ、その後ソースと照合して修正点を記録しなさい。

独立習得基準

独立習得チェック:3 つの終了理由と正確な発火条件、5 段階中 4 つ以上を無援助で述べられること。

公式ソースリソースと権利証跡

linking Open Access / CC BY 4.0

ReAct: Synergizing Reasoning and Acting in Language Models

著者・提供機関: Shunyu Yao, Jeffrey Zhao, Dian Yu, Nan Du, Izhak Shafran, Karthik Narasimhan, Yuan Cao | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 3 & 4 (ReAct Formulation and Synergy)

検証バージョンと日付: ICLR 2023 / arXiv:2210.03629v2 (Verified: 2026-10-30)

選定理由:

Foundational academic paper introducing the interleaved Thought-Action-Observation control loop in LLM agents, establishing formal interleaving of task reasoning and external tool execution.

ソース前提知識:

Prompt engineering, token generation basics, and conversational interaction loops.

安全フォールバック(オフライン回復性):

Complete algorithmic step traces, state loop diagrams, and control loop pseudocode are self-contained in curriculum materials.

比較対象候補リソースと選定評価 (2)

Pure reasoning without environmental actions or external tool grounding; ReAct extends reasoning into an interactive environment action-observation loop.

Early neuro-symbolic modular router concept; ReAct offers simpler, prompting-native action-observation loop dynamics suitable for standard API models.

linking Open Access / CC BY 4.0

Reflexion: Language Agents with Verbal Reinforcement Learning

著者・提供機関: Noah Shinn, Federico Cassano, Ashwin Gopinath, Karthik Narasimhan, Shunyu Yao | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Section 2 & 3 (Reflexion Architecture and Memory Buffer)

検証バージョンと日付: NeurIPS 2023 / arXiv:2303.11366v4 (Verified: 2026-10-30)

選定理由:

Rigorous framework analyzing dynamic error feedback, memory buffering of execution failures, and controlled self-reflection loops without updating model weights.

ソース前提知識:

Reinforcement learning concepts, agent evaluation loops, and error feedback handling.

安全フォールバック(オフライン回復性):

Error recovery state diagrams and retry budget algorithms are detailed in curriculum materials.

比較対象候補リソースと選定評価 (2)

Single-turn generative text refinement; Reflexion operates across multi-turn external environment action-observation failures.

Domain-specific LLM-based agent for open-world Minecraft; Reflexion establishes domain-general verbal feedback control loops.