Skip to main content

Your first query

The simplest way to interact with Claude is using the query() function. Here’s a complete working example:
The query() function is an async function that returns an AsyncIterator of response messages.

Understanding the response

To extract specific content from Claude’s responses, check the message type and content blocks:

Configuring with options

Customize Claude’s behavior using ClaudeAgentOptions:

Using tools

Enable Claude to perform actions like reading and writing files:
By default, Claude will ask for permission before using tools. To auto-accept file edits, set permission_mode='acceptEdits' in your options.

Auto-accepting tool usage

Setting the working directory

Specify a working directory for file operations:

Complete example

Here’s a complete example combining multiple concepts:

Next steps

Client API

Build interactive, bidirectional conversations

Custom tools

Create your own tools for Claude to use

Hooks

Add custom logic to the agent loop

Error handling

Handle errors gracefully in your application