query() function provides a simple, one-shot way to interact with Claude. It’s perfect for basic tasks and quick interactions.
Basic Example
The simplest way to use the SDK is with a basic question:Using Custom Options
You can customize Claude’s behavior usingClaudeAgentOptions:
The
max_turns option limits the number of conversation turns. Setting it to 1 ensures Claude responds only once.Using Tools
You can enable specific tools for Claude to use:Complete Example
Here’s a complete example running all three patterns:Full working example
Full working example
Key Takeaways
- Use
query()for simple, one-shot interactions - Iterate over the async generator to receive messages
- Check message types using
isinstance() - Configure behavior with
ClaudeAgentOptions - Enable tools using the
allowed_toolsparameter
Next Steps
Streaming Mode
Learn about interactive conversations with ClaudeSDKClient
Tool Permissions
Control which tools Claude can use