Common Patterns

Proven patterns for effective agent development and usage.

This guide covers common patterns and approaches for getting the most out of your generated agents.

Effective Prompting

Be Specific

Vague prompts produce vague results. Compare:

1# Bad
2Review this code
3
4# Good
5Review src/auth/login.ts for:
61. Security vulnerabilities (especially injection attacks)
72. Error handling completeness
83. Input validation
9Provide specific line numbers for any issues found.

Provide Context

Give the agent relevant context:

1# Bad
2Fix the bug
3
4# Good
5The login form at src/components/LoginForm.tsx throws
6"Cannot read property 'email' of undefined" when submitted
7with empty fields. The form should show validation errors
8instead of crashing.

Break Down Complex Tasks

Split large tasks into steps:

1# Bad
2Refactor the entire authentication system
3
4# Good
5Let's refactor authentication step by step:
61. First, analyze the current auth implementation
72. Then, identify issues and improvement opportunities
83. Finally, implement changes one module at a time
9Start with step 1: analyze src/auth/ and summarize the current implementation.

Memory (CLAUDE.md) Patterns

Project Context Template

1# Project Context
2
3## Overview
4[Brief description of what this project does]
5
6## Tech Stack
7- Frontend: [framework]
8- Backend: [framework]
9- Database: [database]
10- Infrastructure: [cloud/hosting]
11
12## Key Directories
13- `src/` - Main source code
14- `tests/` - Test files
15- `docs/` - Documentation
16- `scripts/` - Build and deployment scripts

Coding Standards Template

1# Coding Standards
2
3## Style
4- Use TypeScript strict mode
5- Prefer const over let
6- Use async/await over callbacks
7- Maximum line length: 100 characters
8
9## Naming
10- Components: PascalCase (UserProfile.tsx)
11- Functions: camelCase (getUserById)
12- Constants: UPPER_SNAKE_CASE (MAX_RETRY_COUNT)
13- Files: kebab-case (user-profile.tsx)
14
15## Testing
16- Unit tests required for utility functions
17- Integration tests for API endpoints
18- Use Jest and React Testing Library

Slash Command Patterns

Review Command Template

1Review $1 for:
2
3## Security
4- [ ] Input validation
5- [ ] Authentication checks
6- [ ] Authorization checks
7- [ ] Injection vulnerabilities
8- [ ] Sensitive data exposure
9
10## Quality
11- [ ] Error handling
12- [ ] Edge cases
13- [ ] Code duplication
14- [ ] Complexity
15
16## Performance
17- [ ] N+1 queries
18- [ ] Unnecessary computations
19- [ ] Memory leaks
20
21Provide specific findings with line numbers.

Testing Command Template

1Generate tests for $1:
2
31. Identify all public functions and methods
42. For each function:
5   - Write happy path tests
6   - Write edge case tests
7   - Write error handling tests
83. Use Jest and follow existing test patterns
94. Run the tests and fix any failures
10
11Output the test file to the appropriate location.

Workflow Patterns

Analysis Workflow

1{
2  "name": "analyze",
3  "description": "Comprehensive codebase analysis",
4  "steps": [
5    {
6      "name": "inventory",
7      "prompt": "Create an inventory of all files and their purposes"
8    },
9    {
10      "name": "dependencies",
11      "prompt": "Map dependencies between modules",
12      "dependsOn": ["inventory"]
13    },
14    {
15      "name": "issues",
16      "prompt": "Identify potential issues and improvements",
17      "dependsOn": ["dependencies"]
18    },
19    {
20      "name": "report",
21      "prompt": "Generate a summary report",
22      "dependsOn": ["issues"],
23      "output": "ANALYSIS_REPORT.md"
24    }
25  ]
26}

Migration Workflow

1{
2  "name": "migrate",
3  "description": "Safe migration workflow",
4  "steps": [
5    {
6      "name": "backup",
7      "prompt": "Document current state before changes"
8    },
9    {
10      "name": "plan",
11      "prompt": "Create detailed migration plan",
12      "dependsOn": ["backup"]
13    },
14    {
15      "name": "implement",
16      "prompt": "Implement changes incrementally",
17      "dependsOn": ["plan"]
18    },
19    {
20      "name": "verify",
21      "prompt": "Verify changes and run tests",
22      "dependsOn": ["implement"]
23    }
24  ]
25}

Tool Usage Patterns

Search Before Write

Always understand existing code before making changes:

1Before adding the new feature:
21. Search for similar implementations in the codebase
32. Understand the existing patterns
43. Follow the established conventions
54. Then implement the change

Read-Analyze-Act

Structure complex tasks:

  1. Read - Gather all relevant information
  2. Analyze - Understand the situation
  3. Act - Make targeted changes

Error Handling Patterns

Graceful Degradation

When a tool fails, have fallback strategies:

1If web-search fails:
21. Try searching local documentation
32. Check cached information in CLAUDE.md
43. Ask user for guidance

Clear Error Reporting

Configure agents to report errors clearly:

1# In CLAUDE.md
2
3## Error Handling
4When encountering errors:
51. Report the specific error message
62. Explain what was being attempted
73. Suggest potential solutions
84. Ask for guidance if unclear

Performance Patterns

Minimize Tool Calls

Batch related operations:

1# Bad - Multiple tool calls
2Read file A, then read file B, then read file C
3
4# Good - Batched request
5Read files A, B, and C and summarize their relationships

Use Targeted Searches

Be specific with search patterns:

1# Bad - Broad search
2Search for "error" in all files
3
4# Good - Targeted search
5Search for "AuthenticationError" in src/auth/

Iterate and improve

Start with simple patterns and iterate. Monitor how your agent performs and adjust configurations based on real usage.