smolagents — Cursor rule: Python agents and installation
This Cursor rule guides the structure of smolagents applications and flags an unsafe tool pattern. It applies to Python files.
.cursor/rules/smolagents.mdcWhat smolagents does
The rule targets **/*.py and recommends keeping agent logic and tools in separate modules. Its example turns web search into a smolagents tool, with the search logic outside the main agent file.
The guide marks eval as unsafe for calculating expressions. That matters when an agent accepts expressions as input: a calculation tool built this way can execute code rather than just perform arithmetic. Place the rule at .cursor/rules/smolagents.mdc in the project.
What it helps with
- split agent logic and tools into modules
- turn a search function into a smolagents tool
- spot unsafe expression evaluation with
eval
Example prompts for the agent
- Reorganize my smolagents agent so its tools live in separate modules.
- Make this web search function a smolagents tool.
- Check the calculation tool for unsafe use of
eval.
How to install smolagents
Manually: put the files in place
.cursor/rules/smolagents.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/smolagents.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/smolagents.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/smolagents.mdManually: put the files in place
.clinerules/smolagents.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about smolagents
How do I install smolagents in Cursor?
Add the rule at .cursor/rules/smolagents.mdc.
What does the smolagents rule do?
It guides how to separate agent code from its tools and points out the risk of using eval for calculations.
Which files does the rule apply to?
It covers files matching **/*.py.
Source text
smolagents.mdc · CC0-1.0
---
description: Definitive guidelines for building robust, performant, and maintainable AI agents using smolagents, focusing on practical patterns and avoiding common pitfalls.
globs: **/*.py
---
# smolagents Best Practices
This guide outlines the definitive best practices for developing `smolagents` applications. Adhering to these principles ensures your agents are reliable, efficient, and easy to maintain.
## 1. Code Organization and Structure
Organize your agent logic and tools into clear, distinct modules. This improves readability, reusability, and testability.
❌ BAD: Monolithic agent file
```python
# agent.py
from smolagents import CodeAgent, HfApiModel, tool
import requests # Direct import in agent file
@tool
def search_web(query: str) -> str:
# ... complex search logic ...
return "search result"
@tool
def calculate_math(expression: str) -> float:
# ... math logic ...
return eval(expression) # DANGER! Unsafe!
agent = CodeAgent(tools=[search_web, calculate_math], model=HfApiModel())
agent.run("What is 2+2 and also search for 'latest AI news'")
```
✅ GOOD: Modular structure
```python
# src/tools.py
from smolagents import tool
import requests
import json
@tool
def web_search(query: str) -> str:
"""
Performs a web search for the given query.
Args:
query: The search term.
Returns:
A summary of the search results or an error message.
"""
try:
# Replace with a real search API (e.g., DuckDuckGoSearchTool, custom API)
response = requests.get(f"https://api.example.com/search?q
© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc