Input and output
Streaming Input
Streaming Input 这一页讲的,就是 Streaming Input 这件事在 Claude Code 里到底怎么用。
页面信息
这页不是官方原文,而是顺着官方文档结构做的中文解释版。命令、参数、配置名这些硬东西尽量保留,解释部分则尽量讲成人能照着做的话。
如果你碰到特别敏感的配置、权限或企业环境差异,最好顺手点上面的“查看原始文档”再核一遍。
这一页先讲明白
这页主要讲 Streaming Input:Understanding the two input modes for Claude Agent SDK and when to use each
你可以把它当成"Input and output"这块里专门管这一摊事的说明书。
你可以把"Streaming Input"理解成 Input and output 这一栏里的一把专门工具。这页不是让你背书,而是教你什么时候该把这把工具拿出来。
原文这页大多会按 Overview、Streaming Input Mode (Recommended)、How It Works、Benefits 这些环节往下讲。
翻成人话,大概就是:Streaming Input Mode (Recommended)
第一,先别一上来全开全配。先按最小一步试通,确认没跑偏,再继续往下加。
第二,命令、配置名、参数名这些硬东西尽量保留原样。人话解释是帮你听懂,不是帮你改关键字。
第三,照着原文这几个环节挨个过:Overview -> Streaming Input Mode (Recommended) -> How It Works -> Benefits。像下地先看水路、再试机器、再正式开干,一步一步最稳。
原页关键片段:Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
import { readFile } from "fs/promises";
async function* generateMessages() {
// First message
yield {
type: "user" as const,
message: {
role: "user" as const,
content: "Analyze this codebase for security issues"
}
};
// Wait for conditions or user input
await new Promise((resolve) => setTimeout(resolve, 2000));
// Follow-up with image
yield {
type: "user" as const,
message: {
role: "user" as const,
content: [
{
type: "text",
text: "Review this architecture diagram"
},
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: await readFile("diagram.png", "base64")
}
}
]
}
};
}
// Process streaming responses
for await (const message of query({
prompt: generateMessages(),
options: {
maxTurns: 10,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} 原页关键片段:Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
// Simple one-shot query
for await (const message of query({
prompt: "Explain the authentication flow",
options: {
maxTurns: 1,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}
// Continue conversation with session management
for await (const message of query({
prompt: "Now explain the authorization process",
options: {
continue: true,
maxTurns: 1
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} Documentation Index
这里不是让你背"Documentation Index"这个词,而是让你看它真干活时怎么使。
Overview
别把这段只当成标题看,它其实是在给"Overview"划边界。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Streaming Input Mode (Recommended)
看到这里,就把"Streaming Input Mode (Recommended)"当成一件真要上手的活来看。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
How It Works
看懂这里,后面出怪事时你心里会更有底。
Benefits
这一段主要是在把"Benefits"讲实,不是只摆个标题给你看。
Image Uploads
看到这里,就把"Image Uploads"当成一件真要上手的活来看。
Queued Messages
这一块主要是在说"Queued Messages"真到手上该怎么用,哪里最容易踩坑。
Tool Integration
这里不是让你背"Tool Integration"这个词,而是让你看它真干活时怎么使。
这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。
Hooks Support
这一段是在说怎么用 lifecycle hooks 去做 customize behavior at various points。看这种内容,光知道名字没用,还是得落到手上。
如果你打算把外接能力往里挂,这里提到的 hooks、MCP、skills、memory 都要分清各自负责哪一摊。
Real-time Feedback
这一段是在说怎么看 responses as they’re generated, not just final results。将来真出问题时,你就知道该去哪儿翻、翻到什么算正常。
Context Persistence
这里不是让你背"Context Persistence"这个词,而是让你看它真干活时怎么使。
Implementation Example
这里不是让你背"Implementation Example"这个词,而是让你看它真干活时怎么使。
Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
import { readFile } from "fs/promises";
async function* generateMessages() {
// First message
yield {
type: "user" as const,
message: {
role: "user" as const,
content: "Analyze this codebase for security issues"
}
};
// Wait for conditions or user input
await new Promise((resolve) => setTimeout(resolve, 2000));
// Follow-up with image
yield {
type: "user" as const,
message: {
role: "user" as const,
content: [
{
type: "text",
text: "Review this architecture diagram"
},
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: await readFile("diagram.png", "base64")
}
}
]
}
};
}
// Process streaming responses
for await (const message of query({
prompt: generateMessages(),
options: {
maxTurns: 10,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} Single Message Input
看到这里,就把"Single Message Input"当成一件真要上手的活来看。
When to Use Single Message Input
这里不是催你全用上,而是在说 When to Use Single Message Input 什么时候用才划算。
如果你打算把外接能力往里挂,这里提到的 hooks、MCP、skills、memory 都要分清各自负责哪一摊。
Limitations
这一块主要是在说"Limitations"真到手上该怎么用,哪里最容易踩坑。
如果你打算把外接能力往里挂,这里提到的 hooks、MCP、skills、memory 都要分清各自负责哪一摊。
Implementation Example
这里不是让你背"Implementation Example"这个词,而是让你看它真干活时怎么使。
Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
// Simple one-shot query
for await (const message of query({
prompt: "Explain the authentication flow",
options: {
maxTurns: 1,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}
// Continue conversation with session management
for await (const message of query({
prompt: "Now explain the authorization process",
options: {
continue: true,
maxTurns: 1
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} 照着做一遍
如果你不想来回翻,就先照这几步顺着做。
每做完一步就看一下结果,再决定要不要继续往下。
第 1 步:Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
import { readFile } from "fs/promises";
async function* generateMessages() {
// First message
yield {
type: "user" as const,
message: {
role: "user" as const,
content: "Analyze this codebase for security issues"
}
};
// Wait for conditions or user input
await new Promise((resolve) => setTimeout(resolve, 2000));
// Follow-up with image
yield {
type: "user" as const,
message: {
role: "user" as const,
content: [
{
type: "text",
text: "Review this architecture diagram"
},
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: await readFile("diagram.png", "base64")
}
}
]
}
};
}
// Process streaming responses
for await (const message of query({
prompt: generateMessages(),
options: {
maxTurns: 10,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} 第 2 步:Implementation Example
真到动手的时候了,下面这条直接敲一遍,看它回什么。
import { query } from "@anthropic-ai/claude-agent-sdk";
// Simple one-shot query
for await (const message of query({
prompt: "Explain the authentication flow",
options: {
maxTurns: 1,
allowedTools: ["Read", "Grep"]
}
})) {
if (message.type === "result") {
console.log(message.result);
}
}
// Continue conversation with session management
for await (const message of query({
prompt: "Now explain the authorization process",
options: {
continue: true,
maxTurns: 1
}
})) {
if (message.type === "result") {
console.log(message.result);
}
} 一眼看懂这一页
先把这页到底在讲什么看明白,再去碰具体命令和配置,最不容易绕晕。
Streaming Input
|
v
这是 Input and output 里的一摊要紧活
|
v
先弄懂,再下手 文末提醒
这站会按官方 docs 的导航和内容变化继续重生成,原站加页、删页、改页时,这里会跟着更新。
人话解释会尽量顺着原页往下讲,但命令、参数名、配置名这些硬东西还是保留原样,免得你抄过去跑不起来。