Closed Loop Design Skill
Guide for designing closed-loop prompts that ensure agent reliability through feedback loops.
When to Use
-
Designing new agentic workflows
-
Adding validation to existing processes
-
Creating self-correcting agent pipelines
-
Building automated test-fix cycles
Core Concept
Every agentic operation should follow the Request-Validate-Resolve pattern:
REQUEST → VALIDATE → RESOLVE ↑ ↓ └────────────────────┘
Design Workflow
Step 1: Identify the Operation
What task needs to be validated?
-
Code changes (feature, bug fix, refactor)
-
File operations (create, modify, delete)
-
External interactions (API calls, database queries)
-
Build/deploy operations
Step 2: Define Validation Mechanism
How do we know if it succeeded?
Operation Type Validation Mechanism
Code changes Run tests, type check, lint
File operations Verify file exists, check content
API interactions Check response status, validate data
Build operations Build succeeds, no errors
Step 3: Design the Resolve Path
What happens on failure?
-
Analyze: Parse error output to understand failure
-
Fix: Make minimal, targeted corrections
-
Re-validate: Run validation again
-
Limit retries: Set maximum attempts (typically 3-5)
Step 4: Create the Prompt Structure
Template:
Closed Loop: [Operation Name]
Request
[Clear task description with success criteria]
Validate
- Command:
[validation command] - Success: [what success looks like]
- Failure: [what failure looks like]
Resolve (if validation fails)
- Analyze the failure output
- Identify root cause
- Apply minimal fix
- Return to Validate step
- Maximum 3 retry attempts
Example: Test-Fix Loop
Closed Loop: Implement Feature
Request
Implement the user login feature according to spec.
Validate
- Command:
pytest tests/test_auth.py -v - Success: All tests pass (exit code 0)
- Failure: One or more tests fail
Resolve
- Read failing test output
- Identify which test failed and why
- Fix the implementation (not the test)
- Re-run pytest
- Maximum 3 retry attempts
Common Closed Loop Patterns
Code Quality Loop
REQUEST: Write/modify code VALIDATE: Lint + Type check + Tests RESOLVE: Fix errors iteratively
Build Verification Loop
REQUEST: Make changes VALIDATE: Full build succeeds RESOLVE: Fix build errors
E2E Validation Loop
REQUEST: Implement user flow VALIDATE: E2E test passes RESOLVE: Fix failing steps
Best Practices
-
Start with tests: If tests do not exist, consider adding them first
-
Fast validation first: Run quick checks before slow ones
-
Structured output: Use JSON for machine-parseable results
-
Clear failure messages: Include context for resolution
-
Bounded retries: Prevent infinite loops
Memory References
-
@closed-loop-anatomy.md - Full pattern documentation
-
@test-leverage-point.md - Why tests are ideal validators
-
@validation-commands.md - Validation command patterns
Version History
- v1.0.0 (2025-12-26): Initial release
Last Updated
Date: 2025-12-26 Model: claude-opus-4-5-20251101