Week 12 of 12 🏗️

Claude Agent SDK & Building Apps

Claude Agent SDK และการสร้างแอป

Claude Agent SDK 与应用开发

Claude Agent SDK & Building Apps

1. Introduction / บทนำ / 简介

Claude Agent SDK ให้คุณสร้างแอปพลิเคชัน AI ที่กำหนดเองโดยใช้เครื่องมือ Claude Code แบบโปรแกรม มีสำหรับ Python และ TypeScript รองรับการทำงานกับไฟล์ การเข้าถึงเว็บ MCP servers hooks และ subagents — ทั้งหมดจากโค้ดของคุณเอง
Claude Agent SDK 让你以编程方式使用 Claude Code 的工具来构建自定义 AI 应用。它提供 Python 和 TypeScript 版本,支持文件操作、网页访问、MCP servers、hooks 和 subagents——全部都能从你自己的代码中调用。
The Claude Agent SDK lets you build custom AI applications using Claude Code's tools programmatically. Available for Python and TypeScript, it supports file operations, web access, MCP servers, hooks, and subagents — all from your own code.

Fun Facts / ข้อเท็จจริงที่น่าสนใจ / 趣味知识

  • The SDK supports async streaming for real-time output.
  • You can resume previous sessions with their ID.
  • Permission modes control what the agent can do automatically.
A colorful pile of LEGO bricks
An SDK is a box of bricks: standard parts, endless builds. — Photo: Alan Chia, CC BY-SA 2.0, via Wikimedia Commons

2. Vocabulary / คำศัพท์ / 词汇

Click a card to flip it. คลิกการ์ดเพื่อพลิก 点击卡片翻面。

SDK
เอสดีเค
软件开发工具包
Software Development Kit — tools for building apps
API
เอพีไอ
应用程序接口
Application Programming Interface — how programs talk
query
คิวรี
查询
To send a request and get a response
async
อะซิงค์
异步
Running without waiting for completion
stream
สตรีม
Data flowing continuously in real-time
option
ตัวเลือก
选项
A setting that changes behavior
callback
คอลแบ็ก
回调
A function called when an event happens
deploy
ดีพลอย
部署
To make software available for use
pipeline
ไปป์ไลน์
流水线
A series of automated steps
library
ไลบรารี
A collection of pre-written code
import
อิมพอร์ต
导入
To bring in code from a library
production
โปรดักชัน
生产环境
The live environment users interact with

3. Grammar Focus / ไวยากรณ์ / 语法焦点

Future Plans (going to, will, planning to) / การพูดถึงอนาคต (going to, will, planning to) / 将来计划(going to、will、planning to)

Thai:
การพูดถึงอนาคตใช้: 'going to' (แผนที่ตัดสินใจแล้ว), 'will' (ตัดสินใจทันที/แน่นอน), 'planning to' (ความตั้งใจ) ตัวอย่าง: 'We are going to build an agent.' 'The SDK will handle authentication.' 'I'm planning to deploy next week.'
中文:
谈论将来的计划用:'going to'(已计划好的决定),'will'(临时决定/确定的事),'planning to'(打算)。例如:'We are going to build an agent.'(我们打算构建一个 agent。)'The SDK will handle authentication.'(SDK 会处理身份验证。)'I'm planning to deploy next week.'(我计划下周部署。)
English:
Future plans use: 'going to' (planned decision), 'will' (spontaneous/certain), 'planning to' (intention). Example: 'We are going to build an agent.' 'The SDK will handle authentication.' 'I'm planning to deploy next week.'
เรากำลังจะสร้างบอทตรวจสอบโค้ด
我们打算构建一个代码审查机器人。
We are going to build a code review bot.
Agent จะประมวลผลไฟล์ทั้งหมดอัตโนมัติ
Agent 会自动处理所有文件。
The agent will process all files automatically.
ฉันวางแผนจะเพิ่ม MCP servers ใน pipeline
我计划把 MCP servers 加入流水线。
I'm planning to add MCP servers to the pipeline.

Section Check 1

What function sends prompts in the SDK?

ฟังก์ชันอะไรส่ง prompts ใน SDK?

在 SDK 中,哪个函数用来发送 prompts?

Section Check 2

Which expresses a planned decision?

ข้อใดแสดงการตัดสินใจที่วางแผนไว้?

哪个表达已计划好的决定?

Section Check 3

How do you install the Python SDK?

ติดตั้ง Python SDK ยังไง?

如何安装 Python SDK?

4. Tutorial Steps / ขั้นตอนการเรียนรู้ / 教程步骤

1
Install the SDK / ติดตั้ง SDK / 安装 SDK
Run: pip install claude-agent-sdk (Python) or npm install @anthropic-ai/claude-agent-sdk (TypeScript).

How to do it

  • Python: pip install claude-agent-sdk (needs Python 3.10+).
  • TypeScript: npm install @anthropic-ai/claude-agent-sdk.
  • Check it worked: pip show claude-agent-sdk prints the version.
2
Write a basic query / เขียน query พื้นฐาน / 编写基础 query
Import query from the SDK and send a prompt with allowed_tools.

How to do it

  • In a new .py file, import it: from claude_agent_sdk import query.
  • Call query(prompt="...") and loop over the messages it returns — the loop is async, so copy the small starter from the docs the first time.
  • Run it: python myscript.py — you see Claude working, message by message.
3
Add tool restrictions / เพิ่มข้อจำกัดเครื่องมือ / 添加工具限制
Set allowed_tools=['Read', 'Grep'] to limit what the agent can do.

How to do it

  • Pass allowed_tools=['Read', 'Grep'] in the options.
  • Now your agent can look at files but never change them.
  • Start narrow and widen later — the opposite is how accidents happen.
4
Connect MCP servers / เชื่อมต่อ MCP servers / 连接 MCP servers
Pass mcp_servers config to ClaudeAgentOptions for external integrations.

How to do it

  • Pass an mcp_servers config inside ClaudeAgentOptions.
  • Your script then has the same extra tools as your CLI sessions.
  • Same server definitions, just written in code.
5
Deploy your agent / ดีพลอย agent ของคุณ / 部署你的 agent
Package your script and deploy it as a service, CI/CD step, or scheduled task.

How to do it

  • Your agent is a normal script — run it on a schedule (cron), in CI, or behind a small API.
  • Put the API key in an environment variable. Never write it in the code.
  • Log what the agent does, so you can check its work later.

5. Hands-On Activity / กิจกรรมลงมือทำ / 动手练习

แบบฝึกหัด: เขียนสคริปต์ Python ง่ายๆ โดยใช้ Agent SDK ที่อ่านไฟล์และสรุป ใช้ฟังก์ชัน query() พร้อม Read ใน allowed_tools
练习:用 Agent SDK 写一个简单的 Python 脚本,读取一个文件并做摘要。使用 query() 函数,并在 allowed_tools 中加入 Read。
Practice: Write a simple Python script using the Agent SDK that reads a file and summarizes it. Use the query() function with Read in allowed_tools.

Step by step

  1. Install the SDK: pip install claude-agent-sdk.
  2. Ask Claude (in the CLI) to write summarize.py: a script that uses query() with allowed_tools=['Read'] to read and summarize one file.
  3. Run it: python summarize.py.
  4. Change the target file and run it again.

✓ You did it right if: the script prints a short, correct summary of the file — your first own agent.

📜 A complete working summarize.py / ตัวอย่างคำตอบ / 示例答案
# summarize.py - your first agent
# install first:  pip install claude-agent-sdk

import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions, AssistantMessage

async def main():
    options = ClaudeAgentOptions(
        allowed_tools=["Read"],        # look, but never change
    )
    async for message in query(
        prompt="Read README.md and summarize it in 3 sentences.",
        options=options,
    ):
        if isinstance(message, AssistantMessage):
            for block in message.content:
                if hasattr(block, "text"):
                    print(block.text)

asyncio.run(main())

# ---- Run it ----
# $ python summarize.py
# README.md explains how to install the project, how to run
# the tests, and where to report problems.

6. Reference Table / ตารางอ้างอิง / 参考表

Command / ToolEnglishภาษาไทย中文
pip install claude-agent-sdkInstall Python SDKติดตั้ง Python SDK安装 Python SDK
query()Send prompt to agentส่ง prompt ไปยัง agent向 agent 发送 prompt
allowed_toolsRestrict agent toolsจำกัดเครื่องมือ agent限制 agent 可用的工具
permission_modeControl permissionsควบคุมสิทธิ์控制权限
resumeContinue a sessionกลับมาต่อเซสชัน继续之前的会话

7. Interactive Lab / ห้องทดลอง / 互动实验室

Build a tiny agent: choose its tools, run it, watch permissions bite. / สร้าง agent เล็ก ๆ เลือกเครื่องมือ แล้วรันดู / 构建一个小 agent:选工具、运行、看权限生效

Configure your agent, then run it. The tools you allow decide what it can do.

allowed_tools:

8. Summary / สรุป / 总结

Agent SDK นำพลังของ Claude Code มาสู่แอปพลิเคชันของคุณ ใช้ query() ส่ง prompts กำหนดค่าเครื่องมือและสิทธิ์ และสร้าง AI workflows ที่กำหนดเอง Future tenses (going to, will, planning to) แสดงแผนและการคาดการณ์
Agent SDK 把 Claude Code 的能力带进你自己的应用。用 query() 发送 prompts,配置工具和权限,构建自定义 AI 工作流。将来时(going to、will、planning to)用来表达计划和预测。
The Agent SDK brings Claude Code's power to your own applications. Use query() to send prompts, configure tools and permissions, and build custom AI workflows. Future tenses (going to, will, planning to) express plans and predictions.

Homework / การบ้าน / 作业

1. ติดตั้ง Agent SDK ใน Python หรือ TypeScript
2. เขียนสคริปต์ที่ใช้ Claude วิเคราะห์ไฟล์
3. เขียน 5 ประโยคเกี่ยวกับโปรเจกต์ AI ในอนาคตของคุณ
1. 用 Python 或 TypeScript 安装 Agent SDK。
2. 写一个让 Claude 分析文件的脚本。
3. 写 5 个关于你未来 AI 项目的句子。
1. Install the Agent SDK in Python or TypeScript.
2. Write a script that uses Claude to analyze a file.
3. Write 5 sentences about your future AI projects.