langchain-claude-code / claude-sdk
Cursor rule1 starsChanged 12 months agoMITView on GitHub
query() async function for one-shot stateless prompts with ClaudeAgentOptions
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 repository15 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .cursor/rules/claude-sdk/agents.mdcCursor rule
- .cursor/rules/claude-sdk/client.mdcCursor rule
- .cursor/rules/claude-sdk/hooks.mdcCursor rule
- .cursor/rules/claude-sdk/index.mdcCursor rule
- .cursor/rules/claude-sdk/permissions.mdcCursor rule
- .cursor/rules/claude-sdk/tools.mdcCursor rule
- .cursor/rules/claude-sdk/types.mdcCursor rule
- .cursor/rules/index.mdcCursor rule
- .cursor/rules/langchain/callbacks.mdcCursor rule
- .cursor/rules/langchain-claude-code.mdcCursor rule
- .cursor/rules/langchain/custom-chat-model.mdcCursor rule
- .cursor/rules/langchain/langgraph-integration.mdcCursor rule
- .cursor/rules/langchain/streaming.mdcCursor rule
- .cursor/rules/langchain/tool-calling.mdcCursor rule
The file
We split this query.mdc into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are 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.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 8
Use query() for simple, stateless interactions where all inputs are known upfront.
- Very specific: line 18
Use ClaudeSDKClient instead for:
- Distinctive: line 21
When you need interrupt capabilities
≈ “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: query() async function for one-shot stateless prompts with ClaudeAgentOptions
alwaysApply: false
---
# query() - One-Shot Queries
Use `query()` for simple, stateless interactions where all inputs are known upfront.
## When to Use
✓ Simple one-off questions
✓ Batch processing of independent prompts
✓ Code generation or analysis tasks
✓ Automated scripts and CI/CD pipelines
✓ Fire-and-forget operations
Use `ClaudeSDKClient` instead for:
- Interactive conversations with follow-ups
- When you need interrupt capabilities
- Multi-turn sessions with state
## Basic Usage
```python
import anyio
from claude_agent_sdk import query, AssistantMessage, TextBlock
async def main():
async for message in query(prompt="What is the capital of France?"):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(block.text)
anyio.run(main)
```
## With Options
```python
from claude_agent_sdk import query, ClaudeAgentOptions
options = ClaudeAgentOptions(
system_prompt="You are a helpful assistant that explains things simply.",
max_turns=1,
)
async for message in query(
prompt="Explain what Python is in one sentence.",
options=options
):
# Process messages...
pass
```
## With Tools
```python
from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage
options = ClaudeAgentOptions(
allowed_tools=["Read", "Write", "Bash"],
system_prompt="You are a helpful file assistant.",
)
async for message in query(
prompt="Create a file called hello.txt with 'Hello, World!' in it",
options=options,
):
if isinstance(message, ResultMessage) and message.total_cost_usd:
print(f"Cost: ${message.total_cost_usd:.4f}")
```
## Working Directory
```python
from pathlib import Path
from claude_agent_sdk import query, ClaudeAgentOptions
options = ClaudeAgentOptions(
cwd="/path/to/project" # or Path("/path/to/project")
)
async for message in query(prompt="List files here", options=options):
pass
```
## Streaming Mode (Unidirectional)
Send multiple prompts as an async iterable. All prompts are sent, then all responses received.
```python
async def prompts():
yield {"type": "user", "message": {"role": "user", "content": "Hello"}}
yield {"type": "user", "message": {"role": "user", "content": "How are you?"}}
async for message in query(prompt=prompts()):
print(message)
```
## Key Characteristics
- **Unidirectional**: Send all messages upfront, receive all responses
- **Stateless**: Each query is independent
- **Simple**: Fire-and-forget, no connection management
- **No interrupts**: Cannot send follow-up messages mid-stream
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.

