เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Run your first Agent SDK query

The Claude Agent SDK lets you drive Claude Code from a Python script instead of the terminal. Importing query from claude_agent_sdk gives you an async iterator: you pass it a prompt and loop over the messages it streams back as Claude works.

The SDK is already available in this session, and a small orders.py is in your working directory. Complete the script so it asks Claude to summarize that file, then prints each streamed message.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Claude Code in Action

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Inside the async for loop, call query() with a prompt describing the task.
  • Run the main() coroutine by passing it to asyncio.run().

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

import asyncio
from claude_agent_sdk import query


async def main():
    # query() streams messages back as Claude works; loop over them
    async for message in ____:
        print(message)


# Drive the async entry point
____
แก้ไขและรันโค้ด