Step 05: Tool 실행 + Agent Loop
core 120 min

Tool 실행 + Agent Loop

도구 호출 → 실행 → 결과 반환의 전체 Agent Loop을 구현합니다.

Execute this step

Run from step folder:
cd steps/05_tool_execution && cargo run
Run from project root:
cargo run -p step05_tool_execution

Step 5: Tool 실행 + Agent Loop

학습 목표

  • 도구 호출 → 실행 → 결과 반환 루프
  • Agent의 핵심 동작 원리 이해
  • dispatch_tool_call, reply_internal 분석

참조 소스

| 파일 | 내용 | |------|------| | crates/goose/src/agents/agent.rs | reply_internal, dispatch_tool_call |

핵심 개념

Agent Loop는 다음과 같은 사이클을 반복합니다:

  1. User message → LLM에 전달
  2. LLM 응답 분석 → Tool 호출 요청 감지
  3. Tool 실행 (dispatch_tool_call)
  4. Tool 결과 → 다시 LLM에 전달
  5. 최종 응답 또는 추가 Tool 호출
User → LLM → [Tool Call] → Execute → [Result] → LLM → Response
              ↑___________________________________|

Agent Loop 의사 코드

1loop {
2    let response = provider.complete(model, system, &messages, &tools).await?;
3
4    if response.has_tool_calls() {
5        for tool_call in response.tool_calls() {
6            let result = dispatch_tool_call(tool_call).await?;
7            messages.push(Message::tool_result(result));
8        }
9    } else {
10        // Tool 호출이 없으면 루프 종료
11        return response.message;
12    }
13}

루프 종료 조건

  • LLM이 더 이상 Tool을 호출하지 않을 때
  • max_turns에 도달했을 때

이 루프가 goose Agent의 핵심입니다.

소스 코드

1//! Sample Step 5: Tool 실행 + Agent Loop
2
3fn main() {
4    println!("=== Step 5: Tool 실행 + Agent Loop ===\n");
5    println!("TODO: 도구 호출 -> 실행 -> 결과 반환 루프");
6}

체크리스트

  • [ ] Agent Loop 흐름 이해
  • [ ] dispatch_tool_call 메커니즘 분석
  • [ ] reply_internal 코드 읽기
  • [ ] 루프 종료 조건 파악

Did you find this helpful? Give it a cheer!