设置
创建客户端和线程:- Python
- Javascript
- cURL
from langgraph_sdk import get_client
client = get_client(url=<DEPLOYMENT_URL>)
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";
const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
const assistantID = "agent";
const thread = await client.threads.create();
curl --request POST \
--url <DEPLOYMENT_URL>/threads \
--header 'Content-Type: application/json' \
--data '{}'
取消单个运行
以下示例会创建一个运行,然后使用不同选项取消它,并打印运行结果,以展示每种情况下的结果。你可以取消状态为pending 或 running 的运行。尝试取消状态不是 pending 或 running 的运行会导致错误。
使用中断取消(默认)
中断会停止执行该运行的工作进程,并将运行标记为interrupted。不会删除任何内容:
- 运行记录仍然保留(状态为
interrupted)。你可以获取它、检查输入/输出,并查看执行历史记录。 - 该运行的所有检查点都会继续保留。线程在最后一个已完成步骤时的状态也会被保留。
- 之后可以从检查点继续运行(例如,使用时间旅行),也可以检查部分状态。
- Python
- Javascript
- cURL
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
)
await client.runs.cancel(thread["thread_id"], run["run_id"])
run_after = await client.runs.get(thread["thread_id"], run["run_id"], wait=True)
print(run_after["status"]) # "interrupted"
const run = await client.runs.create(
thread["thread_id"],
assistantID,
{ input: { messages: [{ role: "user", content: "长任务" }] } }
);
await client.runs.cancel(thread["thread_id"], run["run_id"], wait=true);
const runAfter = await client.runs.get(thread["thread_id"], run["run_id"]);
console.log(runAfter["status"]); // "interrupted"
# 创建运行(使用响应中的 run_id 和 thread_id)
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "总结这些文档"}]}}'
# 使用默认操作取消(中断)
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/cancel?wait=true
# 获取运行,以查看状态 "interrupted" 并确认运行仍然存在
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>
使用回滚取消
rollback会停止运行,然后从存储中删除该运行及其检查点:- 运行记录会被删除。该运行不再出现在该线程的运行列表或历史记录中。
- 该运行创建的所有检查点都会被删除。线程状态会恢复到运行开始前的状态(就像该运行从未执行过一样)。
- 回滚后无法继续运行或检查该运行。
- Python
- Javascript
- cURL
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
)
await client.runs.cancel(thread["thread_id"], run["run_id"], action="rollback", wait=True)
# 由于运行已被删除,这里会抛出错误
try:
await client.runs.get(thread["thread_id"], run["run_id"])
except Exception:
print("运行已正确删除")
const run = await client.runs.create(
thread["thread_id"],
assistantID,
{ input: { messages: [{ role: "user", content: "长任务" }] } }
);
await client.runs.cancel(thread["thread_id"], run["run_id"], wait=true, action="rollback");
// 由于运行已被删除,这里会抛出错误
try {
await client.runs.get(thread["thread_id"], run["run_id"]);
} catch (e) {
console.log("运行已正确删除");
}
# 创建运行,然后使用回滚取消
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "总结这些文档"}]}}'
curl --request POST \
--url "<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/cancel?action=rollback"
# 由于运行已被删除,这里会抛出错误
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>
等待取消完成
默认情况下,取消请求会在发出取消请求后返回,而运行则会异步取消。wait=True 会让取消请求一直阻塞,直到运行被完全取消。当你希望了解运行取消后的最终状态时,这很有用(例如,创建了哪些检查点、最终输出是什么)。
- Python
- Javascript
- cURL
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
)
# 异步取消运行
await client.runs.cancel(thread["thread_id"], run["run_id"])
# 获取运行状态
run_after = await client.runs.get(thread["thread_id"], run["run_id"])
print(run_after["status"]) # "pending" 或 "running"
# 等待运行被正确取消
await client.runs.join(thread["thread_id"], run["run_id"])
run_after = await client.runs.get(thread["thread_id"], run["run_id"])
print(run_after["status"]) # "interrupted"
const run = await client.runs.create(
thread["thread_id"],
assistantID,
{ input: { messages: [{ role: "user", content: "长任务" }] } }
);
// 异步取消运行
await client.runs.cancel(thread["thread_id"], run["run_id"]);
// 获取运行状态
const runRunning = await client.runs.get(thread["thread_id"], run["run_id"])
console.log(runRunning["status"]) // "pending" 或 "running"
// 等待运行被正确取消
await client.runs.join(thread["thread_id"], run["run_id"])
const runInterrupted = await client.runs.get(thread["thread_id"], run["run_id"])
console.log(runInterrupted["status"]) // "interrupted"
# 创建运行
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "总结这些文档"}]}}'
# 异步取消运行
curl --request POST \
--url "<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/cancel"
# 获取运行状态;在取消完成前应为 "pending" 或 "running",完成后变为 "interrupted"
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>
取消多个运行
使用批量取消端点,可以在一个请求中取消多个运行。同时支持中断和回滚操作。按线程 ID 和运行 ID 取消
传入运行 ID,取消指定的运行。- Python
- Javascript
- cURL
run1 = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "第一个请求"}]},
)
run2 = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "第二个请求"}]},
multitask_strategy="enqueue",
)
await client.runs.cancel_many(
thread_id=thread["thread_id"],
run_ids=[run1["run_id"], run2["run_id"]]
)
# 等待运行被取消
await client.runs.join(thread["thread_id"], run2["run_id"])
runs_after = await client.runs.list(thread["thread_id"])
for run in runs_after:
if run["run_id"] in (run1["run_id"], run2["run_id"]):
print(run["run_id"], run["status"]) # "interrupted"
// Javascript SDK 不支持按运行 ID 批量删除
# 创建两个运行(从每个响应中获取 run_id)
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "第一个请求"}]}}'
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "第二个请求"}]}}'
# 按运行 ID 取消两个运行
curl --request POST \
--url "<DEPLOYMENT_URL>/runs/cancel?action=interrupt" \
--header 'Content-Type: application/json' \
--data '{"thread_id": "<THREAD_ID>", "run_ids": ["<RUN_ID_1>", "<RUN_ID_2>"]}'
# 列出运行以确认结果
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs
按状态取消
取消部署中所有线程里符合指定状态的运行。有效的状态选项为pending、running 或 all。
- Python
- Javascript
- cURL
run1 = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "第一个请求"}]},
)
thread2 = await client.threads.create()
run2 = await client.runs.create(
thread2["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "第二个请求"}]},
)
await client.runs.cancel_many(
status="running",
)
# 等待运行被取消
await client.runs.join(thread2["thread_id"], run2["run_id"])
run_after = await client.runs.get(thread["thread_id"], run1["run_id"])
print(run_after["status"]) # 运行现在为 "interrupted"
run_after2 = await client.runs.get(thread2["thread_id"], run2["run_id"])
print(run_after2["status"]) # 所有线程中的运行都会被取消
// Javascript SDK 不支持按状态批量删除
# 创建一个运行
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "第一个请求"}]}}'
# 创建第二个线程
curl --request POST \
--url <DEPLOYMENT_URL>/threads \
--header 'Content-Type: application/json' \
--data '{}'
# 在第二个线程中创建运行
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID_2>/runs \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "第二个请求"}]}}'
# 取消所有正在运行的运行
curl --request POST \
--url "<DEPLOYMENT_URL>/runs/cancel?action=interrupt" \
--header 'Content-Type: application/json' \
--data '{"status": "running"}'
# 获取运行状态以确认结果
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID_1>
curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID_2>/runs/<RUN_ID_2>
断开连接时取消
启动流式运行或等待运行时,可以设置on_disconnect="cancel",这样客户端断开连接时运行就会被取消。当用户关闭应用或连接丢失时,这可以避免运行一直处于进行中状态。
- Python
- Javascript
- cURL
# 使用 runs.wait:客户端断开连接时取消运行
result = await client.runs.wait(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
on_disconnect="cancel",
)
# 使用 runs.stream:客户端断开连接时取消运行
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
on_disconnect="cancel",
):
print(chunk)
# 使用 runs.join:等待现有运行;客户端断开连接时取消运行
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "长任务"}]},
)
await client.runs.join(
thread["thread_id"],
run["run_id"],
on_disconnect="cancel",
)
# 使用 runs.join_stream:加入现有运行并进行流式处理;客户端断开连接时取消运行
async for chunk in client.runs.join_stream(
thread["thread_id"],
run["run_id"],
on_disconnect="cancel",
):
print(chunk)
// 使用 runs.wait:客户端断开连接时取消运行
const result = await client.runs.wait(
thread["thread_id"],
assistantID,
{ input: { messages: [{ role: "user", content: "长任务" }] }, onDisconnect: "cancel" }
);
// 使用 runs.stream:客户端断开连接时取消运行
const streamResponse = client.runs.stream(
thread["thread_id"],
assistantID,
{ input: { messages: [{ role: "user", content: "长任务" }] }, onDisconnect: "cancel" }
);
for await (const chunk of streamResponse) {
console.log(chunk);
}
// Javascript SDK 不支持使用 runs.join 在断开连接时取消运行
// 使用 runs.joinStream:加入现有运行并进行流式处理;客户端断开连接时取消运行
const joinStreamResponse = client.runs.joinStream(
thread["thread_id"],
run["run_id"],
{ cancelOnDisconnect: true }
);
for await (const chunk of joinStreamResponse) {
console.log(chunk);
}
# runs.wait:创建运行并等待输出;客户端断开连接时取消运行
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/wait \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "长任务"}]}, "on_disconnect": "cancel"}'
# 创建并流式处理运行;客户端断开连接时取消运行
curl --request POST \
--url "<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream?on_disconnect=cancel" \
--header 'Content-Type: application/json' \
--data '{"assistant_id": "agent", "input": {"messages": [{"role": "user", "content": "长任务"}]}}'
# runs.join:等待现有运行;客户端断开连接时取消运行
curl --request GET \
--url "<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join?cancel_on_disconnect=cancel"
# runs.join_stream:加入现有运行并进行流式处理;客户端断开连接时取消运行
curl --request GET \
--url "<DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/stream?cancel_on_disconnect=cancel"
常见场景
- 人在回路和中断:代理可以在中断处暂停,以等待人工输入。取消运行会停止执行;这与中断不同,后者会暂停运行,并且可以使用新输入继续运行。
- 时间旅行:使用
interrupt操作取消后,运行和检查点仍然可用。你可以从检查点继续运行(时间旅行),以重放执行过程或创建执行分支。 - 重复发送消息:当运行正在进行时用户发送新输入,多任务策略(enqueue、reject、interrupt、rollback)会决定现有运行是被中断还是回滚,以及如何处理新运行。若要从应用中显式取消运行,请使用本页面介绍的取消 API。
- Studio:在 Studio 中,使用运行界面里的 Cancel 按钮取消当前运行。
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

