--- title: Checkpointing description: 실행 상태를 자동으로 저장하여 크루, 플로우, 에이전트가 실패 후 재개할 수 있습니다. icon: floppy-disk mode: "wide" --- 체크포인팅은 실행 중 실행 상태의 스냅샷을 저장하여 크루, 플로우, 에이전트가 실패 후 재개하거나 대체 브랜치로 분기될 수 있도록 합니다. 체크포인팅의 작동 방식: 이벤트, 스토리지, 상속. 5분 가이드: 실행, 중단, 재개. 일반적인 워크플로우를 위한 작업 중심 레시피. `CheckpointConfig`, 이벤트, 프로바이더, CLI. ## 설명 ### 체크포인트란 체크포인트는 실행의 특정 시점에 기록된 `RuntimeState`의 직렬화된 스냅샷입니다. 어떤 태스크가 완료되었는지, 그 출력값, 현재 입력값, 그리고 실행을 식별하는 lineage ID를 기록합니다. 체크포인트에서 복원하면 CrewAI는 해당 상태를 재구성하고 이미 완료된 작업을 건너뛰고 계속 진행합니다. 포크하면 CrewAI는 새 lineage 아래에 상태를 복원하여 새 브랜치와 원본 실행이 서로 덮어쓰지 않도록 합니다. ### 체크포인트가 기록되는 시점 체크포인팅은 이벤트 기반입니다. 런타임은 `on_events`로 선택한 이벤트를 구독하고, 이벤트가 발생할 때마다 체크포인트를 기록합니다. 기본값 `task_completed`는 완료된 태스크당 하나의 체크포인트를 생성합니다 — 세분화와 디스크 사용의 합리적인 균형입니다. `llm_call_completed`와 같은 고빈도 이벤트는 더 세밀한 복구를 위해 사용 가능하지만 훨씬 많은 파일을 기록합니다. ### 스토리지 CrewAI에는 두 가지 프로바이더가 포함되어 있습니다: - `JsonProvider`는 체크포인트당 하나의 파일을 기록합니다. 사람이 읽기 쉽고 검사하기 편리합니다. - `SqliteProvider`는 단일 SQLite 데이터베이스에 기록합니다. 고빈도 체크포인팅에 적합합니다. `max_checkpoints`가 설정되면 두 프로바이더 모두 가장 오래된 체크포인트를 자동으로 제거합니다. 체크포인트 기록은 best-effort 방식입니다. 실패한 체크포인트는 로그에 기록되지만 실행을 중단시키지 않습니다. ### 상속 모델 `Crew`, `Flow`, `Agent` 모두 `checkpoint` 인수를 받습니다. 자식은 자체 값을 설정하거나 `False`를 전달하여 옵트아웃하지 않는 한 부모로부터 상속합니다. 크루에서 체크포인팅을 한 번 활성화하면 모든 에이전트가 참여하거나, 특정 에이전트만 선택적으로 제외할 수 있습니다. ## 튜토리얼: 실패한 크루 재개하기 이 가이드는 약 5분이 소요됩니다. 두 개의 태스크가 있는 크루를 실행하고 중간에 종료한 다음, 저장된 체크포인트에서 재개합니다. ```python from crewai import Agent, Crew, Task researcher = Agent(role="Researcher", goal="Research", backstory="Expert") writer = Agent(role="Writer", goal="Write", backstory="Expert") crew = Crew( agents=[researcher, writer], tasks=[ Task(description="Research AI trends", agent=researcher, expected_output="bullets"), Task(description="Write a summary", agent=writer, expected_output="paragraph"), ], checkpoint=True, ) ``` ```python result = crew.kickoff() ``` 첫 번째 태스크가 완료된 후 `Ctrl+C`를 누릅니다. `./.checkpoints/` 디렉토리에서 `_.json` 형식의 파일이 체크포인트입니다. ```python from crewai import CheckpointConfig result = crew.kickoff( from_checkpoint=CheckpointConfig( restore_from="./.checkpoints/_.json", ), ) ``` 연구 태스크는 건너뛰고, 작성자는 저장된 연구 출력에 대해 실행되며, 크루가 완료됩니다. ## 사용 방법 ```python crew = Crew(agents=[...], tasks=[...], checkpoint=True) ``` `task_completed` 이벤트마다 `./.checkpoints/`에 기록합니다. ```python from crewai import Crew, CheckpointConfig crew = Crew( agents=[...], tasks=[...], checkpoint=CheckpointConfig( location="./my_checkpoints", on_events=["task_completed", "crew_kickoff_completed"], max_checkpoints=5, ), ) ``` ```python JsonProvider from crewai import Crew, CheckpointConfig from crewai.state import JsonProvider crew = Crew( agents=[...], tasks=[...], checkpoint=CheckpointConfig( location="./my_checkpoints", provider=JsonProvider(), max_checkpoints=5, ), ) ``` ```python SqliteProvider from crewai import Crew, CheckpointConfig from crewai.state import SqliteProvider crew = Crew( agents=[...], tasks=[...], checkpoint=CheckpointConfig( location="./.checkpoints.db", provider=SqliteProvider(), max_checkpoints=50, ), ) ``` SQLite는 동시 읽기를 위해 WAL 저널 모드를 활성화합니다. 고빈도 체크포인팅에는 SQLite를 선호하세요. ```python crew = Crew( agents=[ Agent(role="Researcher", ...), Agent(role="Writer", ..., checkpoint=False), ], tasks=[...], checkpoint=True, ) ``` ```python config = CheckpointConfig(restore_from="./my_checkpoints/.json") crew = Crew.from_checkpoint(config) result = crew.kickoff() ``` `fork()`는 새 lineage 아래에 체크포인트를 복원하여 새 실행이 원본과 충돌하지 않도록 합니다. ```python config = CheckpointConfig(restore_from="./my_checkpoints/.json") crew = Crew.fork(config, branch="experiment-a") result = crew.kickoff(inputs={"strategy": "aggressive"}) ``` `branch` 레이블은 선택 사항이며, 생략하면 자동 생성됩니다. ```python crew = Crew( agents=[researcher, writer], tasks=[research_task, write_task, review_task], checkpoint=CheckpointConfig(location="./crew_cp"), ) ``` 기본 트리거: `task_completed`. ```python from crewai.flow.flow import Flow, start, listen from crewai import CheckpointConfig class MyFlow(Flow): @start() def step_one(self): return "data" @listen(step_one) def step_two(self, data): return process(data) flow = MyFlow( checkpoint=CheckpointConfig( location="./flow_cp", on_events=["method_execution_finished"], ), ) result = flow.kickoff() config = CheckpointConfig(restore_from="./flow_cp/.json") flow = MyFlow.from_checkpoint(config) result = flow.kickoff() ``` ```python agent = Agent( role="Researcher", goal="Research topics", backstory="Expert researcher", checkpoint=CheckpointConfig( location="./agent_cp", on_events=["lite_agent_execution_completed"], ), ) result = agent.kickoff(messages=[{"role": "user", "content": "Research AI trends"}]) ``` 모든 이벤트에 핸들러를 등록하고 `state.checkpoint()`를 호출합니다. ```python Sync from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallCompletedEvent @crewai_event_bus.on(LLMCallCompletedEvent) def on_llm_done(source, event, state): path = state.checkpoint("./my_checkpoints") print(f"체크포인트 저장: {path}") ``` ```python Async from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallCompletedEvent @crewai_event_bus.on(LLMCallCompletedEvent) async def on_llm_done_async(source, event, state): path = await state.acheckpoint("./my_checkpoints") print(f"체크포인트 저장: {path}") ``` 핸들러가 세 개의 매개변수를 받을 때 `state` 인수가 자동으로 제공됩니다. 전체 이벤트 카탈로그는 [Event Listeners](/ko/concepts/event-listener) 문서를 참조하세요. ```bash crewai checkpoint # .checkpoints/ 또는 .checkpoints.db 자동 감지 crewai checkpoint --location ./my_checkpoints crewai checkpoint --location ./.checkpoints.db ``` Checkpoint TUI 왼쪽 패널은 체크포인트를 브랜치별로 그룹화하며, 포크는 부모 아래에 중첩됩니다. 체크포인트를 선택하면 메타데이터, 엔티티 상태, 태스크 진행 상황이 표시됩니다. **Resume**은 실행을 계속하고, **Fork**는 새 브랜치를 시작합니다. 세부 정보 패널에는 두 개의 편집 가능한 영역이 있습니다: - **Inputs** — 원래 kickoff의 입력으로, 미리 채워져 있으며 편집 가능합니다. - **태스크 출력** — 완료된 태스크의 출력. 출력을 편집하고 **Fork**를 누르면 다운스트림 태스크가 무효화되어 수정된 컨텍스트로 다시 실행됩니다. "what if" 탐색에 유용합니다: 포크, 조정, 관찰. ```bash crewai checkpoint list ./my_checkpoints crewai checkpoint info ./my_checkpoints/.json crewai checkpoint info ./.checkpoints.db ``` ## 레퍼런스 ### `CheckpointConfig` 스토리지 대상. `JsonProvider`는 디렉토리, `SqliteProvider`는 데이터베이스 파일 경로. 체크포인트를 트리거하는 이벤트 타입. [이벤트 타입](#이벤트-타입) 참조. 스토리지 백엔드. `JsonProvider` 또는 `SqliteProvider`. 보관할 최대 체크포인트 수. 각 기록 후 가장 오래된 것이 제거됩니다. `from_checkpoint`를 통해 전달될 때 복원할 체크포인트. ### `checkpoint` 필드 값 `Crew`, `Flow`, `Agent`에서 사용 가능. 부모에서 상속. 기본값으로 활성화. 명시적 옵트아웃. 상속을 중단합니다. 사용자 정의 설정. ### 이벤트 타입 `on_events`에 대한 일반적인 값: | 사용 사례 | 이벤트 | |:----------|:-------| | 각 태스크 완료 후 | `["task_completed"]` | | 각 플로우 메서드 완료 후 | `["method_execution_finished"]` | | 에이전트 실행 완료 후 | `["agent_execution_completed"]`, `["lite_agent_execution_completed"]` | | 크루 완료 시에만 | `["crew_kickoff_completed"]` | | 모든 LLM 호출 후 | `["llm_call_completed"]` | | 모든 이벤트 | `["*"]` | `["*"]` 및 `llm_call_completed`와 같은 고빈도 이벤트는 많은 체크포인트를 기록하고 성능을 저하시킬 수 있습니다. `max_checkpoints`와 함께 사용하세요. ### 스토리지 프로바이더 체크포인트당 하나의 파일, `location` 내부에 `_.json` 형식으로 명명. WAL 저널링이 있는 `location`의 단일 데이터베이스 파일. ### CLI | 명령 | 목적 | |:-----|:-----| | `crewai checkpoint` | TUI 실행; 스토리지 자동 감지. | | `crewai checkpoint --location ` | 특정 위치에 대해 TUI 실행. | | `crewai checkpoint list ` | 체크포인트 나열. | | `crewai checkpoint info ` | 체크포인트 파일 또는 SQLite 데이터베이스의 최신 항목 검사. |