Files
2nd/10_Wiki/Topics/Domain_Programming/AI_and_ML/Tool-Usage-Optimization.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

6.3 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-tool-usage-optimization Tool Usage Optimization 10_Wiki/Topics verified self
Function Calling
Tool Use
Agent Tools
none A 0.9 applied
llm
tool-use
function-calling
agentic
2026-05-10 pending
language framework
Python Anthropic SDK / OpenAI SDK / MCP

Tool Usage Optimization

매 한 줄

"매 LLM의 외부 capability 의 invoke". Tool use (function calling) 매 LLM 이 external API/code/DB 의 structured call 하는 패러다임. 2023 OpenAI function calling → 2024 parallel tools → 2025 MCP standard → 2026 매 every agent 의 backbone. 매 quality 매 tool 정의의 crisp / parallel call / error handling / cache strategy 에 의해 결정.

매 핵심

매 핵심 mechanics

  • Schema — 매 JSON Schema 로 tool input 정의 — 매 model 이 fill.
  • Choiceauto / tool (forced) / none.
  • Parallel — 매 single turn 에 multiple tool calls (Claude / GPT-4o+).
  • Iterative loop — 매 tool result 의 feed back → continue → repeat.

매 design principles

  • Crisp descriptions — 매 tool description 매 "use this when X, not when Y" 명시.
  • Few well-named tools > 매 many overlapping — 매 selection error rate 의 reduce.
  • Idempotent / safe — 매 retry-safe — 매 LLM 매 retry 함.
  • Structured errors — 매 tool error 매 LLM 이 recover 할 수 있게 actionable.

매 modern (2025-2026)

  • MCP (Model Context Protocol) — 매 Anthropic 이 2024-11 release. 매 client/server tool sharing standard. 매 Claude Desktop, Cursor, Windsurf 모두 support.
  • Tool result caching — 매 expensive tool (DB query, web fetch) 매 cache + hash check.
  • Tool budget — 매 agent 의 max-call limit 의 prevent runaway.

매 응용

  1. Code agents (file ops, shell, search).
  2. Customer support (ticket, KB, account API).
  3. Data analysis (SQL, plot, fetch).

💻 패턴

Anthropic tool definition

tools = [{
    "name": "get_weather",
    "description": "Get current weather. Use ONLY for weather; do not use for forecasts beyond 24h.",
    "input_schema": {
        "type": "object",
        "properties": {
            "location": {"type": "string", "description": "City, country (e.g. 'Seoul, KR')"},
            "unit": {"type": "string", "enum": ["c", "f"], "default": "c"},
        },
        "required": ["location"],
    },
}]

resp = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=2000,
    tools=tools,
    messages=[{"role": "user", "content": "Weather in Seoul?"}],
)

Parallel tool execution

import asyncio

async def run_tool(call):
    return await TOOLS[call.name](**call.input)

# 매 LLM이 매 multiple tool_use blocks 의 emit
calls = [b for b in resp.content if b.type == "tool_use"]
results = await asyncio.gather(*(run_tool(c) for c in calls))
# 매 모든 result 의 single user message 로 feed back

Tool loop (agentic)

messages = [{"role": "user", "content": user_query}]
for _ in range(MAX_TURNS):
    resp = client.messages.create(model="claude-opus-4-7", tools=tools, messages=messages)
    messages.append({"role": "assistant", "content": resp.content})
    if resp.stop_reason != "tool_use":
        break
    tool_results = [
        {"type": "tool_result", "tool_use_id": b.id,
         "content": str(TOOLS[b.name](**b.input))}
        for b in resp.content if b.type == "tool_use"
    ]
    messages.append({"role": "user", "content": tool_results})

Structured error

def search_db(query: str):
    try:
        return {"ok": True, "rows": db.execute(query).fetchall()}
    except SQLError as e:
        return {"ok": False, "error": str(e),
                "hint": "Check column names; use `\\d table` to inspect."}

Result caching

import hashlib, json, functools

@functools.lru_cache(maxsize=1024)
def _cached_fetch(url_hash: str, url: str):
    return requests.get(url, timeout=10).text

def web_fetch(url: str):
    return _cached_fetch(hashlib.sha256(url.encode()).hexdigest(), url)

Prompt cache + tools (Anthropic)

resp = client.messages.create(
    model="claude-opus-4-7",
    tools=tools,                       # 매 tools 의 cacheable
    system=[{"type": "text", "text": SYSTEM, "cache_control": {"type": "ephemeral"}}],
    messages=messages,
)

MCP server (Python)

from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-tools")

@mcp.tool()
def query_orders(customer_id: str) -> list[dict]:
    """Return recent orders for a customer."""
    return db.fetch_orders(customer_id)

if __name__ == "__main__":
    mcp.run()

매 결정 기준

상황 Approach
Few tools, in-app Direct SDK tools
Many shared tools across apps MCP server
Latency-critical Pre-fetch / parallel tools
Expensive tools Cache by input hash
Untrusted LLM output Validate + sandbox tool exec
Long agent loop Tool budget + checkpoint

기본값: 매 5-15 well-named tools, parallel calling enabled, structured errors, prompt caching on system + tool definitions.

🔗 Graph

🤖 LLM 활용

언제: 매 external state / capability 의 필요한 모든 LLM app — search, DB, email, code exec, API call. 언제 X: 매 pure text generation (summary, translate) — 매 tool 매 unnecessary.

안티패턴

  • Vague tool names: "do_thing" — 매 LLM 매 selection 의 fail.
  • Too many tools: 매 30+ tool 매 confuse — 매 group / route.
  • No retry on transient error: 매 502 / timeout 매 transient — 매 retry-safe + LLM 의 hint.
  • Streaming tool output to user mid-call: 매 tool result 매 internal — final assistant text 만 user 에.
  • No max-turn limit: 매 infinite loop 의 risk.

🧪 검증 / 중복

  • Verified (Anthropic tool use docs, OpenAI function calling, MCP spec 2024-11, parallel tool calling).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — modern tool use + MCP + caching patterns