Skip to content
BENIBIX Learn

query() Function

AI🧩 Claude Agent SDKAgent SDK Basics

Also known as query(), Agent SDK query, Claude Agent SDK query()

The main function of the kit. Give it a prompt, and it runs the agent and sends back each step.

Think of it like

It is like giving a helper a job. They tell you each step.

Example

Leo calls query() with the prompt find and fix the bug in utils.py. His loop prints each tool Claude uses, then the result.

Why it matters

It is the one way in to the agent, so most programs start here.

Definition

query() is the main function of the Agent SDK. You pass it a prompt and an options object, and it starts the agent loop. It returns a stream you read with a loop, and each item is a message: a tool call, a tool result, some text, or the final result. One call can run many turns before it ends.

Example code

typescript
for await (const m of query({ prompt: "Fix the failing test" })) {
  if (m.type === "result") console.log(m.result);
}
Run one task and print the final result.
The messages one query() call yields.

Common mistake

Waiting for one return value. query() streams messages, so read them in a loop.

Key takeaways

  1. 1
    Prompt and options in. Messages out.
  2. 2
    Runs the whole loop. Many turns.
  3. 3
    Read it in a loop. async for.

Compare