Lộ trình học›Claude API›Building with the Claude API Mở bản đồ

6.73 · Environment inspection — Agent phải "nhìn" được

Bài viếtTrung cấp → Nâng15 phút đọc

Agent perform action — không biết kết quả nếu không inspect.

Sau bài này, bạn sẽ
  1. Hiểu tại sao environment inspection critical cho agents
  2. Pattern: "read before write"
  3. System prompt instruct Claude inspect after actions
  4. Apply for file, UI, API contexts

Mở đầu: Agent bị "bịt mắt"

Agent: edit_file("main.py", old="x=1", new="x=2")
Result: "Edited"

Agent's knowledge:
  "I changed x=1 to x=2"

Actual state:
  main.py may have multiple "x=1". Which was replaced?
  Or syntax error introduced?
  Or file didn't have "x=1" at all?

Without read after write, Claude assumes action worked. Often wrong → downstream failures.

Example: Computer Use

Claude clicks button:

Claude: computer_click(x=450, y=300)
Result: "Click sent"

What happened?

  • Did page navigate?
  • Did dialog open?
  • Did page just load something?
  • Did click miss the button?

Answer: screenshot after action.

Computer Use always pairs click → screenshot. Claude sees result → adapts.

Pattern: Read before write

Ví dụ: File edit

User: Add a new route to main.py

Bad agent:
  edit_file("main.py", old="routes = [", new="routes = [\n  new_route,")
  ← may create syntax error if pattern different than assumed

Good agent:
  1. read_file("main.py") ← inspect first
  2. [sees actual structure]
  3. edit with exact pattern that matches
  4. read_file("main.py") ← verify

Read before write = safe.

System prompt for inspection

Nudge Claude inspect via system prompt:

system = """You are a coding assistant. Follow these rules:

1. ALWAYS read files before editing — never guess content.
2. After edits, re-read to verify changes applied correctly.
3. If action fails or result unexpected, investigate before retry.
4. When running commands, check output carefully for errors.
"""

Sets expectation Claude will inspect.

Tools that enable inspection

Files

  • read_file(path) — see content
  • ls(dir) — see what exists
  • grep(pattern, path) — find specific content

Processes

  • get_output(command_id) — read logs of long-running
  • check_status(job_id) — is it done?

UIs

  • screenshot() — see current state
  • get_element_text(selector) — read value

APIs

  • get_status(resource_id) — latest state
  • list_resources() — inventory

Case study: Video creation agent

Agent creates video with FFmpeg. Need verify quality.

Without inspection

1. generate_script(topic) → script
2. text_to_speech(script) → audio
3. generate_images(script) → images
4. ffmpeg_combine(...) → video file

Agent: "Done, video created."
Reality: Audio/video misaligned, typo in script, images wrong...

With inspection

system = """Video creation agent. Always verify:
- After generating script, read it aloud mentally — catches typos
- After audio, sample length — matches expected duration?
- After images, check count + describe each to confirm right
- After video, extract frames at intervals → verify content
- Use bash+whisper.cpp → generate captions → verify dialogue placement
"""

Claude iterate: verify each stage before proceed. Final video higher quality.

Inspection granularity

Fine-grained

Every action followed by verify:

  • Pros: Catch errors early
  • Cons: Many turns, expensive

Coarse-grained

Verify periodically or at end:

  • Pros: Fewer turns
  • Cons: Errors propagate

Smart (recommended)

Verify high-stakes actions + batch verify low-stakes.

High-stakes (verify each):
  - Edit production code
  - Delete files
  - Send emails

Low-stakes (batch verify):
  - Format whitespace
  - Rename variables (trivial)

Hidden costs — overhead

Inspection tools tốn tokens. Budget:

Task: edit 10 lines
Actions: 10 reads + 10 edits + 10 verify reads = 30 tool calls

vs batch:
1 read → understand → 10 edits → 1 final read = 12 tool calls

Fix: Instruct Claude batch when safe.

Observability for user

Show inspection in UI:

Agent status:
  [✓] Read main.py
  [✓] Analyzed structure
  [⏳] Editing routes...
  [ ] Verifying changes...
  [ ] Running tests...

User sees progression. Trust builds.

Anti-patterns

No verify after action

"I think it worked" = fragile assumption.

Fix: System prompt instruct verify.

Verify everything

30 verify calls for 10 edits = unnecessarily slow.

Fix: Smart — high-stakes only.

Ignore inspection results

Claude reads "Error in line 5" and continues anyway.

Fix: Prompt "If verification shows issue, stop and investigate."

Circular inspection

Read → edit → read → edit → read... indefinitely.

Fix: max_turns. After N reads without progress, escalate.

Ví dụ code structure

SYSTEM = """Agent for codebase maintenance.

Workflow for edits:
1. read_file(path) first, understand context
2. Propose edit with exact old_str (unique in file)
3. edit_file(path, old_str, new_str)
4. read_file(path) to verify change correct
5. If verify fails, investigate + retry

Never edit without reading first.
Never assume tests pass — run them explicitly.
"""


async def run_agent(task: str):
    messages = [{"role": "user", "content": task}]
    
    for turn in range(30):  # agent may need many turns
        response = chat(messages, system=SYSTEM, tools=tools)
        messages.append({"role": "assistant", "content": response.content})
        
        if response.stop_reason == "end_turn":
            return text_from_message(response)
        
        if response.stop_reason == "tool_use":
            results = await run_tools(response)
            messages.append({"role": "user", "content": results})

System prompt + right tools = agent can inspect.

Áp dụng ngay

Bài tập 1: Add inspection to your agent (20 phút)

Take Module 9 reminder agent. Add:

  • Tool list_reminders()
  • System prompt: "Before set_reminder, list existing to avoid duplicates"

Test: "Remind me about doctor tomorrow" when such reminder already exists.

Bài tập 2: Computer Use simulation (optional, advanced)

Design agent với screenshot() + click(x, y) tools. Even simulated, force Claude pattern: click → screenshot → decide next click.

Tóm tắt bài học

Những điều cốt lõi
  • Environment inspection critical — agent blind without.
  • "Read before write" pattern.
  • System prompt nudges Claude inspect.
  • Smart granularity: verify high-stakes, batch low-stakes.
  • Observability UI shows inspection → user trust.
Kiểm tra nhanh3 câu hỏi · chọn đáp án để xem giải thích
    Bạn đã hoàn thành bài này chưa?Tiến độ lưu trên trình duyệt của bạn.