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

6.71 · Agents và tools — Abstract vs specialized

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

Agent success = 80% quality tools + 20% prompt. Bad tools → no amount of prompt engineering fix.

Sau bài này, bạn sẽ
  1. Hiểu tại sao abstract tools > specialized cho agents
  2. Design tool set combinable cho agent flexibility
  3. Study Claude Code tool set as model
  4. Avoid tool bloat

Mở đầu: Tools = agent capability

Good tools → Claude creative combine for countless tasks.

Abstract > specialized

Anti-pattern: Specialized tools

Tools:
- refactor_code()
- install_dependencies()
- fix_typescript_error()
- deploy_to_aws()
- ...50 specific actions

Problems:

  • Claude can't adapt when task slightly different
  • Tool maintenance burden
  • Prompt bloat (list all 50 schemas)
  • Combinations locked in

Good pattern: Abstract tools

Tools:
- bash(command)       # Run anything
- read(file)          # Read anything
- write(file, text)   # Create anything
- edit(file, old, new) # Modify anything
- grep(pattern)       # Search anything

Claude combines:

  • "Refactor code" = read → analyze → edit (multiple)
  • "Install deps" = bash(pip install ...)
  • "Fix TS error" = read → understand → edit
  • "Deploy" = bash(AWS CLI commands)

6 tools → unlimited task types.

Case study: Claude Code tool set

Claude Code (production agent) has:

ToolPurpose
ReadView file contents
EditString replace in file
WriteCreate/overwrite file
BashRun shell commands
GlobFind files by pattern
GrepSearch file contents
WebFetchFetch URL
TaskSpawn subagent
TodoWriteTrack multi-step progress

9 tools handle:

  • Debugging
  • Refactoring
  • Adding features
  • Running tests
  • Git operations
  • Documentation
  • Research

No specialized "refactor_code" or "run_tests". Claude figures out.

Tool design principles

1. Verb + noun name

  • read_file
  • file
  • get

2. Single responsibility

Each tool does 1 thing. Combine for complex.

  • read_file(path)
  • read_and_edit_file(path, old, new) (too much)

3. Natural composition

Output of 1 tool = input of another.

  • glob("*.py") → list of paths → read_file(path) → content → grep(pattern, content)

4. Descriptive errors

Claude reads errors to retry:

raise ValueError("File not found: /path/to/file. Existing files: [...]")

Claude learn from error message.

5. Return string or structured

Structured data easy for Claude to reason.

Tool set example: Social media video agent

tools = [
    "bash",           # FFmpeg, misc commands
    "generate_image", # Image from prompt
    "text_to_speech", # TTS API
    "post_media",     # Upload to platform
    "read_file",      # Check generated files
    "get_trends"      # Fetch trending topics
]

Claude can:

  • Research trend + write script + generate voice + generate images + combine video + post
  • Or: user approve image first → proceed
  • Or: iterate on script → then generate

Multi-paths from 6 tools.

Anti-pattern: Tool bloat

Agent with 50 tools:

Problems:

  • Prompt tokens massive (each schema 50-200 tokens × 50 = 2.5K-10K overhead)
  • Claude decision slower
  • Similar tools confuse selection
  • Maintenance nightmare

Threshold: < 15 tools generally. > 20 = refactor.

Solution: Tiered tools

If you really need many tools, tier them:

Primary agent:
  Tools: [search_docs, ask_specialist, escalate]

Specialist "code reviewer" sub-agent:
  Tools: [read, edit, lint, run_tests]

Specialist "deployment" sub-agent:
  Tools: [build, deploy, monitor, rollback]

Primary routes to specialist. Each specialist tractable tool count.

Claude Code Task tool spawns sub-agent — same pattern.

Abstraction layers

Low-level (primitive)

  • bash — run any command

Mid-level (convenience)

  • run_tests — wrap bash("pytest ...") with standard args

High-level (workflow)

  • deploy_to_staging — multi-step workflow wrapped

Use mid/high for repeated patterns. Low for flexibility.

Default: Primitive. Add convenience only if Claude struggles với primitives.

Tool integration with workflow patterns

Agent tools combine with workflow patterns:

Chaining within agent

Claude: tool 1 → tool 2 → tool 3. Effectively chaining within 1 agent.

Parallel tools

Multiple tool calls trong 1 turn → parallel execution (if supported).

Routing via tools

Tool route_to(specialist) → hand off task.

Environment inspection

Tools include read/get_state — Claude check before act (bài 6.73).

Debugging agents

Agent behavior varies. Debug:

1. Log all tool calls

def log_tool_call(name, input, result):
    logger.info({
        "timestamp": now(),
        "agent_session": session_id,
        "tool": name,
        "input": input,
        "result_preview": str(result)[:100]
    })

Replay sessions later.

2. Tool call tree visualization

Session XYZ
├─ read_file(main.py)         [200ms]
├─ grep("TODO")                [100ms]
├─ edit(main.py, ...)          [150ms]
└─ bash("pytest")              [2s]

Understand flow at glance.

3. Success metrics per tool

Track error rates per tool. High errors → tool design issue.

Best practice: Eval agent behavior

Can't test all behaviors of agent. Sample:

test_scenarios = [
    "Fix bug in auth.py",
    "Add logging to main",
    "Deploy to staging",
    # 20 diverse scenarios
]

for scenario in test_scenarios:
    session = run_agent(scenario)
    # Grade:
    # - Completed task? (human verify)
    # - Number of tool calls
    # - Cost
    # - Correct final state

Track over time as add tools / update prompts.

Anti-patterns

Tool = reimplement existing API

get_user(id) wrapping requests.get(...) exactly.

Fix: MCP server hoặc wrap meaningfully.

Tool hide important error

try:
    do_work()
except:
    return "Done"  # Lie

Claude doesn't know action failed → bad chains.

Fix: Surface errors honestly.

Tools with hidden coupling

Tool A must call before B, not documented.

Fix: Tools independent. Or document requirement in description.

Agent without max_turns limit

Infinite loops = money burn.

Fix: Always max_turns (bài 6.35-6.36).

Áp dụng ngay

Bài tập 1: Design tool set (30 phút)

Cho app của bạn, design 6-10 tools cho agent:

  • List candidates
  • Check abstraction level
  • Identify composition paths

Bài tập 2: Reduce tool count (20 phút)

Nếu list > 10 tools, identify:

  • Duplicates (similar tools merge)
  • Specializations → abstract
  • Rarely used (drop)

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

Những điều cốt lõi
  • Abstract tools > specialized cho agents.
  • Claude Code mẫu: 9 tools handle countless tasks.
  • 5 design principles: verb+noun, single responsibility, composition, errors, structured return.
  • Tool bloat at > 15 tools — tier with sub-agents.
  • Debug: log + tree visualization + eval scenarios.
Kiểm tra nhanh2 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.