Overview
Instead of manually repeating actions in a focused_action step, usestart_loop and end_loop_iteration to automate repetitive tasks:
- Learn once, replay many: The agent maps out one iteration, then the system replays it automatically
- Access loop items: Use the special
{{loop_item}}runtime variable to reference the current item in a focused_action step - Structured results: Get detailed summaries of all iterations including successes and failures
- Cache friendly: Loops work seamlessly with trajectories for lightning-fast execution
- Error handling: Loops exit early if errors occur, returning partial results
Actions
start_loop
Begin a loop iteration over a non-empty array or positive integer count. Parameters:text: JSON array like["item1", "item2", "item3"]or integer like"5"for counting loops
- System creates a loop context with all items
- Sets current iteration to 0 (first item)
- Agent completes ONE full iteration manually
- Agent calls
end_loop_iterationwhen done - System automatically replays remaining iterations
text contains no items to process, the loop is skipped instead of started. That includes values like [], null, an empty string, or 0 / negative integer counts. In that case there is no active loop context, so you should continue with the steps after the loop and not call end_loop_iteration.
The {{loop_item}} Variable:
Access the current loop item using the special {{loop_item}} runtime variable:
- Simple items:
Type {{loop_item}}→ types “Alice” during iteration 0 - Nested objects:
{{loop_item.name}}→ accesses name field - Array elements:
{{loop_item[0]}}→ accesses first array element - Deep nesting:
{{loop_item.user.emails[0]}}→ accesses nested data
end_loop_iteration
Mark the current iteration as complete and trigger replay of remaining iterations. Parameters:text: Summary of this iteration (can use runtime variables for dynamic info)
- System evaluates runtime variables in the summary text
- Adds iteration result to the results array
- If iteration 0: Captures trajectory steps and replays iterations 1 to N-1
- If recovery iteration: Replays from current + 1 to N-1
- Returns JSON summary of all iterations when complete
skip_loop_iteration
Skip the rest of the current loop iteration and continue to the next loop item. This is intended for focused agents inside a loop context (i.e., when afocused_action is running during an active start_loop). Use it when the current loop item is not actionable (missing data, not found on screen, wrong state, etc.) and you want to move on gracefully.
Parameters:
text: Brief reason why the iteration is being skipped (this will appear in the loop summary)
- The system records the skip reason for the current iteration
- Any remaining steps for that iteration are not executed
- The loop proceeds to the next item (remaining iterations continue as usual)
- The final loop summary includes
status: "skipped"entries and askippedcount
Loop Types
Array Loop
Iterate over a collection of items. Example - Loop over names:Integer Loop
Repeat actions a fixed number of times. The{{loop_item}} variable will be 0, 1, 2, etc. Positive integers create that many iterations; 0 or a negative count skips the loop entirely.
Example - Download 5 reports:
Variable-Based Loop
Loop over data collected during the workflow using runtime or input variables. Example - Loop over runtime variable:Passing Complex JSON Arrays via SDK
When creating a run programmatically, you can pass arrays of complex objects as input variables. The workflow can then loop over these objects and access nested fields. Workflow Prompt:- TypeScript
- Python
Advanced Patterns
Nested Data Access
When looping over objects or arrays, access nested fields: Example:Combining With Async Extraction
Extract data asynchronously while looping for maximum efficiency: Example:Error Handling in Loops
Loops exit early if errors occur, returning partial results: Example:early_exit: true.
Cache Detection in Loops
By default, recorded loop steps use cache detection.end_loop_iteration and skip_loop_iteration are control steps, so they do not use cache detection.
You can disable cache detection for an individual recorded loop step if a step is too noisy or
causes unnecessary cache misses.
Enabling Loop Cache Detection
In the trajectory editor, cache detection is on by default for recorded loop steps that have a reference screenshot. Toggle Cache detection off to force replay for that step without a screenshot comparison.end_loop_iteration and skip_loop_iteration are control steps and do not use cache detection.
Steps added later in the UI also cannot use cache detection because they have no reference screenshot.
Recovery Options
If cache detection fails mid-loop, the recovery agent is invoked with loop context and can:| Option | Use When | Result |
|---|---|---|
resume_trajectory | Minor issue (popup, timing) | Retries cache detection |
end_loop_iteration | You completed the iteration manually | Loop continues with remaining items |
skip_loop_iteration | This item is problematic | Skips to next item |
declare_task_failed | Workflow is broken | Task fails cleanly |
resume_trajectory, end_loop_iteration, or skip_loop_iteration), the trajectory is still saved.
If the agent calls declare_task_failed, no trajectory is saved.
Loop cache detection is optional per step. Leave it on for steps where visual state matters, and turn it off for steps that are noisy or do not need a screenshot check.
Important Limitations
Complete Examples
Example 1: Discover and Process Patients (Runtime Variable Loop)
This example demonstrates a common pattern where the loop items are discovered during the run rather than passed as input. The agent extracts a list of patients from the screen and then loops over them. Workflow Prompt:- Agent logs in and navigates to the appointments page
focused_actionanalyzes the screen and extracts patient data, saving it as{{todays_patients}}:start_loopbegins iterating over this runtime-discovered array- Agent completes iteration 0 (Alice Smith) manually
- System automatically replays iterations 1-2 for Bob and Carol
- Each iteration accesses nested fields:
{{loop_item.id}},{{loop_item.name}},{{loop_item.appointment_time}}
focused_action during execution and stored as a runtime variable. This is powerful for workflows where you need to “find what’s on the screen, then process each item.”
Example 2: Batch Download Forms
Workflow Prompt:- Iteration 0: Agent downloads form 0
- Iterations 1-9: System automatically downloads forms 1-9
- Terminal command lists all downloaded files
- Total time: ~10 seconds (cached) vs ~5 minutes (uncached)
Example 3: Data Entry From Spreadsheet
Workflow Prompt:Best Practices
Use focused_action for validation in loops
Use focused_action for validation in loops
Add focused_action steps within your loop to verify each iteration succeeded:This ensures errors are caught immediately per iteration.
Parameterize summaries with runtime variables
Parameterize summaries with runtime variables
Make iteration summaries dynamic by using runtime variables:This creates rich per-iteration results you can analyze later.
Prefer small, focused loop bodies
Prefer small, focused loop bodies
Keep loop iterations concise (5-15 steps). For complex processing:Let focused_action handle complex logic per iteration.
Use runtime variables for conditional logic
Use runtime variables for conditional logic
Set flags during iterations for later decision-making:
Troubleshooting
Loop doesn't complete all iterations
Loop doesn't complete all iterations
Cause: An error occurred in one of the iterations.Solution: Check the loop summary’s
iteration_results array to see which iteration failed and why. Add focused_action validation steps to catch errors early.Loop is skipped immediately
Loop is skipped immediately
Cause: The
start_loop input resolved to no items, such as [], null, an empty string, or 0.Solution: Confirm the input or runtime variable really contains a JSON array (or a positive integer for counting loops). If it is intentionally empty, skip the loop body and continue with the steps that come after the loop. Do not call end_loop_iteration when no loop was started.{{loop_item}} shows as literal text
{{loop_item}} shows as literal text
Cause: Runtime variable replacement failed or loop wasn’t started.Solution: Ensure
start_loop was called before using {{loop_item}}. Check that the loop items array is valid JSON.Nested loop error
Nested loop error
Cause: Attempted to call
start_loop while already in a loop.Solution: Complete the current loop with end_loop_iteration before starting a new loop. Nested loops are not supported in the current version.Loop reminder appears repeatedly
Loop reminder appears repeatedly
Cause: You haven’t called
end_loop_iteration after 20+ steps.Solution: Call end_loop_iteration when the iteration is complete. The reminder appears every 20 steps to prevent accidental unclosed loops.Related Tools
Focused Action
Dynamic validation and data extraction within loop iterations
Runtime Variables
Set and use runtime variables in loop summaries
Extract Prompt
Vision-based extraction with async processing in loops
Trajectories 101
How caching accelerates loop execution