agentleFS
thehumanworksthehumanworks/langchain-claude-code/.cursor/rules/claude-sdk/query.mdc

langchain-claude-code / claude-sdk

Cursor rule1 starsChanged 12 months agoMITView on GitHub

query() async function for one-shot stateless prompts with ClaudeAgentOptions

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
  1. Very specific:

    Use query() for simple, stateless interactions where all inputs are known upfront.

    line 8
  2. Very specific:

    Use ClaudeSDKClient instead for:

    line 18
  3. Distinctive:

    When you need interrupt capabilities

    line 21

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
query.mdc
---
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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.