Files
2nd/10_Wiki/Topic_Programming/Coding/AI_Function_Calling_Deep.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

6.2 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
ai-function-calling-deep Function Calling 심화 — Tool Use / 멀티 step Coding draft B conceptual 2026-05-09 2026-05-09
ai
llm
function-calling
tool-use
vibe-coding
language applicable_to
TS / OpenAI / Anthropic
Backend
function calling
tool use
parallel tool calls
tool result
ReAct

Function Calling Deep

LLM 이 함수 호출 결정 → 너가 실행 → 결과 다시 LLM. 반복 루프가 agent 의 핵심. Parallel tool calls / streaming + tools / 재귀 호출 제한.

📖 핵심 개념

  • Tool: name + description + JSON Schema input.
  • LLM 이 "이 tool 호출해" 응답 → 너가 실행 → 결과를 message 로 다시.
  • 종료 조건: stop_reason = end_turn / max_iterations.

💻 코드 패턴

Anthropic tool use loop

import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();

const tools = [
  {
    name: 'search',
    description: 'Search the web',
    input_schema: {
      type: 'object',
      properties: { query: { type: 'string' } },
      required: ['query'],
    },
  },
  {
    name: 'fetch_page',
    description: 'Fetch contents of a URL',
    input_schema: {
      type: 'object', properties: { url: { type: 'string' } }, required: ['url'],
    },
  },
] as const;

async function executeTool(name: string, input: any): Promise<string> {
  if (name === 'search') return JSON.stringify(await google.search(input.query));
  if (name === 'fetch_page') return await fetch(input.url).then(r => r.text());
  throw new Error(`unknown tool: ${name}`);
}

async function agentLoop(userMsg: string, maxIters = 10) {
  const messages: Anthropic.Messages.MessageParam[] = [{ role: 'user', content: userMsg }];

  for (let i = 0; i < maxIters; i++) {
    const r = await client.messages.create({
      model: 'claude-opus-4-7',
      max_tokens: 4096,
      tools, messages,
    });

    messages.push({ role: 'assistant', content: r.content });

    if (r.stop_reason === 'end_turn') {
      // 최종 답
      const text = r.content.find(b => b.type === 'text')?.text;
      return text;
    }

    if (r.stop_reason === 'tool_use') {
      // Parallel tool calls 가능 — 모든 tool_use 처리
      const toolUses = r.content.filter(b => b.type === 'tool_use');
      const toolResults = await Promise.all(toolUses.map(async (t) => ({
        type: 'tool_result' as const,
        tool_use_id: t.id,
        content: await executeTool(t.name, t.input),
      })));
      messages.push({ role: 'user', content: toolResults });
      continue;
    }

    break;
  }
  throw new Error('max iterations');
}

Parallel tool calls (한 turn 안 여러 tool)

LLM 이 한 번에 여러 tool 호출 → 병렬 실행 → 결과 한꺼번에 보냄.

OpenAI 스타일

const r = await openai.chat.completions.create({
  model: 'gpt-4o',
  messages,
  tools: [
    { type: 'function', function: { name: 'search', parameters: {...} } },
    { type: 'function', function: { name: 'fetch_page', parameters: {...} } },
  ],
  tool_choice: 'auto', // 또는 'required' / specific
  parallel_tool_calls: true,
});

if (r.choices[0].finish_reason === 'tool_calls') {
  for (const call of r.choices[0].message.tool_calls!) {
    const result = await executeTool(call.function.name, JSON.parse(call.function.arguments));
    messages.push({
      role: 'tool', tool_call_id: call.id, content: result,
    });
  }
  // 다시 모델 호출
}

Streaming + tools (delta accumulation)

const stream = await client.messages.stream({ ..., tools });

let toolUses: Map<number, { id: string; name: string; input: string }> = new Map();
for await (const ev of stream) {
  if (ev.type === 'content_block_start' && ev.content_block.type === 'tool_use') {
    toolUses.set(ev.index, { id: ev.content_block.id, name: ev.content_block.name, input: '' });
  }
  if (ev.type === 'content_block_delta' && ev.delta.type === 'input_json_delta') {
    const tu = toolUses.get(ev.index)!;
    tu.input += ev.delta.partial_json;
  }
}
// stream 끝 후 toolUses 의 input string → JSON.parse → 실행

Tool 에러 처리

async function safeExecute(name: string, input: any) {
  try {
    return { content: await executeTool(name, input), is_error: false };
  } catch (e) {
    return { content: `Error: ${(e as Error).message}`, is_error: true };
  }
}

// LLM 에 에러도 내용으로 — 자체 복구 시도
toolResults.push({ type: 'tool_result', tool_use_id: t.id, content: result.content, is_error: result.is_error });

Tool 강제 (force tool_choice)

{ tool_choice: { type: 'tool', name: 'extract_recipe' } }

특정 tool 만 호출 → structured output 비슷.

Tool 입력 검증

import { z } from 'zod';

const SearchInput = z.object({ query: z.string().min(1).max(200) });

async function executeTool(name: string, raw: unknown) {
  if (name === 'search') {
    const input = SearchInput.parse(raw); // 잘못된 input 차단
    return search(input.query);
  }
}

Cost 제어

  • maxIters 5-10.
  • Tool result 크기 제한 (truncate to 4kb).
  • 동시 tool 수 제한.
  • 비싼 tool (GPU inference) 은 cache.

🤔 의사결정 기준

작업 추천
1 step + JSON 결과 Structured output (force tool)
멀티 step 추론 Tool loop
검색 + 답변 Web search tool + RAG 결합
Code execution Sandbox (E2B / Daytona)
File 작업 Filesystem tool + 권한 제한
여러 LLM 통합 Vercel AI SDK / LangChain

안티패턴

  • maxIters 무한: LLM 이 무한 loop — 재귀 token 폭발.
  • Tool 없는 description: LLM 이 못 고름.
  • Input schema 너무 복잡: 잘못 호출.
  • Side-effect tool 멱등 X: 재시도 시 중복.
  • Tool result 크기 무제한: 다음 turn 비싸짐.
  • Error 숨김: LLM 못 복구.
  • Auth 없는 tool: 사용자 권한으로 임의 호출.

🤖 LLM 활용 힌트

  • Tool description 풍부, input schema 작게.
  • Parallel tool calls 활용.
  • maxIters + cost cap + tool error 다시 LLM 으로.

🔗 관련 문서