Week 12 of 12 🏗️
Claude Agent SDK และการสร้างแอป
Claude Agent SDK 与应用开发
Click a card to flip it. คลิกการ์ดเพื่อพลิก 点击卡片翻面。
What function sends prompts in the SDK?
ฟังก์ชันอะไรส่ง prompts ใน SDK?
在 SDK 中,哪个函数用来发送 prompts?
Which expresses a planned decision?
ข้อใดแสดงการตัดสินใจที่วางแผนไว้?
哪个表达已计划好的决定?
How do you install the Python SDK?
ติดตั้ง Python SDK ยังไง?
如何安装 Python SDK?
How to do it
pip install claude-agent-sdk (needs Python 3.10+).npm install @anthropic-ai/claude-agent-sdk.pip show claude-agent-sdk prints the version.How to do it
.py file, import it: from claude_agent_sdk import query.query(prompt="...") and loop over the messages it returns — the loop is async, so copy the small starter from the docs the first time.python myscript.py — you see Claude working, message by message.How to do it
allowed_tools=['Read', 'Grep'] in the options.How to do it
mcp_servers config inside ClaudeAgentOptions.How to do it
Step by step
pip install claude-agent-sdk.summarize.py: a script that uses query() with allowed_tools=['Read'] to read and summarize one file.python summarize.py.✓ You did it right if: the script prints a short, correct summary of the file — your first own agent.
# 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.
| Command / Tool | English | ภาษาไทย | 中文 |
|---|---|---|---|
pip install claude-agent-sdk | Install Python SDK | ติดตั้ง Python SDK | 安装 Python SDK |
query() | Send prompt to agent | ส่ง prompt ไปยัง agent | 向 agent 发送 prompt |
allowed_tools | Restrict agent tools | จำกัดเครื่องมือ agent | 限制 agent 可用的工具 |
permission_mode | Control permissions | ควบคุมสิทธิ์ | 控制权限 |
resume | Continue a session | กลับมาต่อเซสชัน | 继续之前的会话 |
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.