Pause and Resume CrewAI Flows #3092
Unanswered
kuldeepsinghkarki
asked this question in
Q&A
Replies: 1 comment
-
|
This is achievable with external state persistence. Here's the pattern: State-Based Checkpointingimport json
def save_flow_state(flow_id, state):
with open(f"checkpoints/{flow_id}.json", "w") as f:
json.dump(state, f)
def load_flow_state(flow_id):
try:
with open(f"checkpoints/{flow_id}.json") as f:
return json.load(f)
except FileNotFoundError:
return None
# Your flow state
state = {
"flow_id": "flow_123",
"current_stage": "stage_2",
"completed_stages": ["stage_1"],
"stage_outputs": {
"stage_1": {...}
},
"paused": False
}Resume Logicdef run_flow(flow_id):
state = load_flow_state(flow_id) or initialize_new_state(flow_id)
# Skip completed stages
stages = ["stage_1", "stage_2", "stage_3"]
start_from = stages.index(state["current_stage"])
for stage in stages[start_from:]:
if state.get("paused"):
save_flow_state(flow_id, state)
return "Flow paused"
# Run stage with previous outputs as context
result = run_stage(stage, state["stage_outputs"])
state["stage_outputs"][stage] = result
state["completed_stages"].append(stage)
state["current_stage"] = stages[stages.index(stage) + 1] if stage != stages[-1] else "completed"
# Checkpoint after each stage
save_flow_state(flow_id, state)Why This Works
More on state-based workflows: https://github.com/KeepALifeUS/autonomous-agents |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Can we pause and resume Crewai flows later on. I tried using flow level persistence and then resumed a flow by passing the flow id, however it always starts from the beginning. Whereas i need the flexibility to start from the method/stage where it was paused. any help/direction would be highly appreacited.
Beta Was this translation helpful? Give feedback.
All reactions