extractum-skills / rules
Cursor rule44 starsChanged 10 months agoMITView on GitHub
Async/await patterns for Claude Agent SDK
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository7 other files it gives its agents.
- .cursor/rules/00-project-overview.mdcCursor rule
- .cursor/rules/05-solution-design.mdcCursor rule
- .cursor/rules/10-python-standards.mdcCursor rule
- .cursor/rules/20-type-annotations.mdcCursor rule
- .cursor/rules/30-logging-standards.mdcCursor rule
- .cursor/rules/40-docstrings.mdcCursor rule
- .cursor/rules/50-error-handling.mdcCursor rule
The file
We split this 60-async-patterns.mdc into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 13
Use asyncio.run() for the main entry point:
- Distinctive: line 60
Always include proper return type hints for async functions:
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
description: "Async/await patterns for Claude Agent SDK"
globs:
- "**/*.py"
alwaysApply: false
priority: 5
---
# Async/Await Patterns
## Main Entry Point
Use `asyncio.run()` for the main entry point:
```python
async def main() -> None:
"""Main entry point for the agent script."""
logger: logging.Logger = setup_logging()
try:
result: AgentResultSummary = await run_agent(DEFAULT_PROMPT, logger)
log_execution_summary(result, logger)
except Exception as e:
logger.error(f"Failed to run agent: {e}")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())
```
---
## Async Generator Processing
Process async generators with `async for`:
```python
async def run_agent(prompt: str, logger: logging.Logger) -> AgentResultSummary:
"""Run the Claude agent with the given prompt."""
options: ClaudeAgentOptions = _create_agent_options()
result_summary: AgentResultSummary = _create_empty_result(prompt)
async for message in query(prompt=prompt, options=options):
text_content: str = extract_text_from_message(message)
if isinstance(message, AssistantMessage):
_process_assistant_message(message, text_content, result_summary, logger)
elif isinstance(message, ResultMessage):
_process_result_message(message, text_content, result_summary, logger)
return result_summary
```
---
## Type Hints for Async
Always include proper return type hints for async functions:
```python
# ✅ Good - Explicit async return type
async def run_agent(prompt: str, logger: logging.Logger) -> AgentResultSummary:
...
async def main() -> None:
...
# The return type is the actual value, not a coroutine
# Python handles the coroutine wrapping automatically
```
---
## Claude Agent SDK Query Pattern
The `query()` function is an async generator that yields messages:
```python
from claude_agent_sdk import (
ClaudeAgentOptions,
Message,
AssistantMessage,
ResultMessage,
SystemMessage,
query,
)
async for message in query(prompt=prompt, options=options):
if isinstance(message, AssistantMessage):
# Handle assistant response
pass
elif isinstance(message, ResultMessage):
# Handle final result with cost/usage info
pass
elif isinstance(message, SystemMessage):
# Handle system messages
pass
```
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

