langchain-claude-code / claude-sdk
Cursor rule1 starsChanged 12 months agoMITView on GitHub
ClaudeSDKClient for interactive multi-turn streaming conversations
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 4,373 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/hooks.mdcCursor rule
- .cursor/rules/claude-sdk/index.mdcCursor rule
- .cursor/rules/claude-sdk/permissions.mdcCursor rule
- .cursor/rules/claude-sdk/query.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 client.mdc into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
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 ClaudeSDKClient for bidirectional, stateful conversations with full control over message flow.
- Very specific: line 19
Use query() instead for:
≈ “Common” is an estimate: at least 1 in 5 of the 7,324 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: ClaudeSDKClient for interactive multi-turn streaming conversations
alwaysApply: false
---
# ClaudeSDKClient - Interactive Sessions
Use `ClaudeSDKClient` for bidirectional, stateful conversations with full control over message flow.
## When to Use
✓ Chat interfaces or conversational UIs
✓ Interactive debugging or exploration sessions
✓ Multi-turn conversations with context
✓ Real-time applications with user input
✓ When you need interrupt capabilities
✓ Custom tools and hooks
Use `query()` instead for:
- Simple one-off questions
- Batch processing
- Stateless operations
## Basic Streaming
```python
from claude_agent_sdk import ClaudeSDKClient, AssistantMessage, TextBlock
async with ClaudeSDKClient() as client:
await client.query("What is 2+2?")
async for msg in client.receive_response():
if isinstance(msg, AssistantMessage):
for block in msg.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
```
## Multi-Turn Conversation
```python
async with ClaudeSDKClient() as client:
# First turn
await client.query("What's the capital of France?")
async for msg in client.receive_response():
display_message(msg)
# Second turn - follow-up (context preserved)
await client.query("What's the population of that city?")
async for msg in client.receive_response():
display_message(msg)
```
## With Options
```python
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
options = ClaudeAgentOptions(
allowed_tools=["Read", "Write"],
system_prompt="You are a helpful coding assistant.",
env={"ANTHROPIC_MODEL": "claude-sonnet-4-5"},
)
async with ClaudeSDKClient(options=options) as client:
await client.query("Create a simple hello.txt file")
async for msg in client.receive_response():
display_message(msg)
```
## Interrupt Capability
```python
import asyncio
async with ClaudeSDKClient() as client:
await client.query("Count from 1 to 100 slowly")
async def consume_messages():
async for msg in client.receive_response():
display_message(msg)
consume_task = asyncio.create_task(consume_messages())
# Wait then interrupt
await asyncio.sleep(2)
await client.interrupt()
await consume_task
# Send new instruction after interrupt
await client.query("Just tell me a quick joke")
async for msg in client.receive_response():
display_message(msg)
```
## Dynamic Control
Change settings during conversation:
```python
async with ClaudeSDKClient() as client:
# Start with default permissions
await client.query("Help me analyze this codebase")
async for msg in client.receive_response():
pass
# Switch to auto-accept edits
await client.set_permission_mode('acceptEdits')
await client.query("Now implement the fix we discussed")
async for msg in client.receive_response():
pass
# Switch model mid-conversation
await client.set_model('claude-sonnet-4-5')
await client.query("Optimize this solution")
```
## Server Info
```python
async with ClaudeSDKClient() as client:
info = await client.get_server_info()
if info:
print(f"Commands available: {len(info.get('commands', []))}")
print(f"Output style: {info.get('output_style', 'default')}")
```
## Manual Connection Management
```python
from claude_agent_sdk import ClaudeSDKClient, CLIConnectionError
client = ClaudeSDKClient()
try:
await client.connect()
await client.query("Hello")
async for msg in client.receive_response():
display_message(msg)
except CLIConnectionError as e:
print(f"Connection error: {e}")
finally:
await client.disconnect()
```
## Key Methods
| Method | Description |
|--------|-------------|
| `connect()` | Establish connection |
| `disconnect()` | Close connection |
| `query(prompt)` | Send a new request |
| `receive_response()` | Yield messages until ResultMessage |
| `receive_messages()` | Yield all messages indefinitely |
| `interrupt()` | Send interrupt signal |
| `set_permission_mode(mode)` | Change permission mode |
| `set_model(model)` | Change AI model |
| `get_server_info()` | Get server capabilities |
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.

