VoltAgent Best Practices provides guidelines for using the VoltAgent framework effectively. It is designed for developers working on AI agent projects who need to understand conventions and patterns.
$ npx skills add https://github.com/voltagent/skills --skill voltagent-best-practicesVoltAgent Best Practices is a skill that documents recommended patterns and conventions for developers working with the VoltAgent framework. It covers key architectural topics including agent design, workflow management, memory implementation, and server configuration. This skill helps developers understand how to structure and implement AI agents effectively using VoltAgent's core patterns. By following these guidelines, developers can build more maintainable and efficient agent projects that align with framework conventions. The skill is part of the official VoltAgent skills collection and supports AI coding agents like Claude Code and Cursor.
Install using the command: `$ npx skills add https://github.com/voltagent/skills --skill voltagent-best-practices`
Choosing between Agent or Workflow for tasks
Creating multi-step pipelines with suspend/resume functionality
Understanding VoltAgent conventions and patterns
$ npx skills add https://github.com/voltagent/skills --skill voltagent-best-practicesgit clone https://github.com/voltagent/skillsCopy the install command above and run it in your terminal.
Launch Claude Code, Cursor, or your preferred AI coding agent.
Use the prompt template or examples below to test the skill.
Adapt the skill to your specific use case and workflow.
Provide best practices for using VoltAgent in [PROJECT_TYPE] projects. Focus on [SPECIFIC_TOPIC, e.g., state management, error handling, or agent orchestration]. Include examples of common pitfalls and how to avoid them. Assume I'm using VoltAgent [VERSION] in [COMPANY]'s [INDUSTRY] workflow.
# VoltAgent Best Practices for State Management
## Key Guidelines
### 1. **Use Immutable State Where Possible**
Avoid mutating state directly in agents. Instead, use VoltAgent’s `update_state()` method to ensure traceability and debugging ease. Example:
```python
# ❌ Avoid: Direct mutation
agent.state.counter += 1
# ✅ Prefer: Immutable update
agent.update_state(counter=agent.state.counter + 1)
```
### 2. **Leverage State Hooks for Side Effects**
Use `on_state_change` hooks to trigger actions like logging or external API calls when state updates. This keeps your agents reactive without cluttering core logic.
```python
@agent.on_state_change('counter')
def log_counter_change(old, new):
print(f"Counter updated from {old} to {new}")
```
### 3. **Validate State Transitions**
Define allowed state transitions in your agent’s schema to prevent invalid workflows. For example, restrict a `payment_processing` agent from skipping the `validation` step.
```python
class PaymentAgent(VolAgent):
state_schema = {
'status': ['pending', 'validated', 'processing', 'completed', 'failed'],
'valid_transitions': [('pending', 'validated'), ('validated', 'processing')]
}
```
## Common Pitfalls
- **Overwriting State**: Never replace `agent.state` entirely. Use `update_state()` to merge changes.
- **Unbounded State Growth**: Trim unused state keys with `agent.trim_state(['old_key1', 'old_key2'])` to avoid memory bloat.
Need help debugging a specific state issue? Share your agent’s state structure and error logs!
Take a free 3-minute scan and get personalized AI skill recommendations.
Take free scan