LangSmith Prompt 管理全攻略, AI智能体开发框架,AI智能体开发必看。
在 LangSmith 里“管理 Prompt”一般有三层含义对应三种用法在 LangSmith UI 上写、改、版本化和发布 PromptPlayground Environments Tags Webhook。在 LangGraph Studio 里直接改图节点里的 Prompt并回写到 LangSmith。在 Python/TS 后端用 SDK 做自动化的版本管理、拉取/推送和缓存。下面给你一个总览再分别讲“怎么用”。一、管理方式全景选路径看这个UI 上写/改/发版 Prompt在 LangGraph 图里改节点 Prompt后端自动化管理要进 Git 审计/CI CD需求你要做啥?LangSmith UI: Playground Prompts Environments TagsStudio: Direct Node Editing / PlaygroundPython TS SDK: push pull tags cacheWebhook 中间件 同步到 GitHub发布到 Staging 或 Production 并在代码里拉取二、LangSmith UI 里的 Prompt 管理版本、环境、权限1) 用 Playground 创建 / 编辑 Prompt在 LangSmith 左侧进入 “Playground”在左侧编辑消息System / Human / AI / Tool / Chat / Messages List可用 f-string 或 Mustache 模板加{variable}或{{variable}}右侧会显示示例输入框便于测试。也可配置结构化输出output schema、工具Tool甚至让 Polly 帮你生成/优化 Prompt。保存时点 Save填名字保存内容包括所选模型与配置。保存后可在左侧 Prompts 列表查看所有 Prompt。2) 版本、环境与标签Staging/Production打开某个 Prompt 的“详情页”后你会看到左侧提交历史每次保存是一个 commit与环境Staging / Production。右侧当前 commit 的详情右上角有 Diff 可对比前后版本。核心能力Environments环境Staging / Production 是两个“命名的部署目标”用预留的标签 staging/production 管理。可以把任意 commit “Promote推进”到 Staging 或 Production悬停 commit 点击 Promote选择环境确认。推进到 Production 不影响它还在 Staging也可对某个环境做 Rollback回滚。Commit tags提交标签给某个 commit 打标签比如 v1.0、golden-v1、prod-backup也可以把已有标签移到别的 commit 上。代码里用标签引用 Prompt而不是写死 commit hashprompt client.pull_prompt(joke-generator:production)等价于使用该标签当前指向的那个 commit。权限与 Owner每个提示可以设“Workspace authorized users”或“Owners only”Owners 可以控制谁能打标签、推环境、删提示。在 More → Access and Permissions 里设置。Webhook提交后自动触发在 Prompts 页面点 Webhook配置一个接收 URL每次有 Prompt commit 就会 POST JSON含 prompt_id/name/commit_hash/manifest 等。典型用途触发 CI/CD、同步到 GitHub、发通知。3) Public Prompt Hub在左侧 Prompts 里可以进入 “Browse all Public Prompts in the LangChain Hub”。可以浏览/搜索/复制/运行公开提示也可以用 SDK 直接拉取公开提示到代码里。三、在 LangGraph Studio 里管理 Prompt改节点里的提示如果你用 LangGraph或 Agent Server做后端Studio 是最顺手的提示调试入口。1) Direct Node Editing直接在图上改节点 Prompt在图的配置里对字段声明langgraph_nodes与langgraph_typeprompt例如system_prompt:strField(defaultYou are a helpful AI assistant.,json_schema_extra{langgraph_nodes:[call_model],langgraph_type:prompt,},)这样 Studio 知道该字段是提示字段并绑定到哪些节点。Studio 中点击节点上的齿轮图标打开配置弹窗直接编辑 system_prompt 等“提示字段”保存后会更新当前 Assistant 版本或新建一个。2) Playground在 Studio 里测试单次 LLM 调用在一个 thread 里点击 View LLM Runs选中某一次 LLM 调用进入 Playground。在 Playground 里改提示、模型/工具配置、测不同参数然后把改好的提示“复制回图”。3) 把节点示例加进 Dataset便于评测Studio 可以把线程里某些节点的历史数据添加到 LangSmith Dataset用作评测/回归测试的样本。四、用 SDK 做自动化管理Python 侧如果你要“后端自动化”可以直接用 LangSmith SDKPython/TS管理提示。1) 安装与初始化pipinstall-Ulangsmith# 建议 0.1.99fromlangsmithimportClient clientClient()前提环境变量设置 LANGSMITH_API_KEY在 LangSmith Settings → API Keys 创建。2) 推送 Promptpush_prompt最简单推一个 ChatPromptTemplatefromlangsmithimportClientfromlangchain_core.promptsimportChatPromptTemplate clientClient()promptChatPromptTemplate.from_template(tell me a joke about {topic})urlclient.push_prompt(joke-generator,objectprompt)print(url)# 会在 UI 里打开该提示推一个带模型的链prompt | model把模型配置也存进去fromlangsmithimportClientfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_openaiimportChatOpenAI clientClient()modelChatOpenAI(modelgpt-5.4-mini)promptChatPromptTemplate.from_template(tell me a joke about {topic})chainprompt|model client.push_prompt(joke-generator-with-model,objectchain)推 StructuredPrompt带输出 schema 的提示也支持单独或连同模型一起推。3) 拉取 Promptpull_prompt普通拉取promptclient.pull_prompt(joke-generator)modelChatOpenAI(modelgpt-5.4-mini)chainprompt|model chain.invoke({topic:cats})拉取“带模型配置”的提示chainclient.pull_prompt(joke-generator-with-model,include_modelTrue)chain.invoke({topic:cats})按版本拉指定 commit hash 或 tagpromptclient.pull_prompt(joke-generator:12344e88)# 按 commit hashpromptclient.pull_prompt(joke-generator:production)# 按 tag官方示例拉取公开 Hub 中的提示需要写 handlepromptclient.pull_prompt(efriis/my-first-prompt)4) 缓存与性能Prompt cacheLangSmith SDK 默认带一个全局的 in-memory 提示缓存stale-while-revalidate默认 100 条、TTL 5 分钟、每 60 秒刷新一次。你可以读取缓存指标命中/未命中/命中率fromlangsmithimportClientfromlangsmith.prompt_cacheimportprompt_cache_singleton clientClient()promptclient.pull_prompt(joke-generator)# 第一次走 API 并缓存promptclient.pull_prompt(joke-generator)# 后续命中缓存print(fCache hits:{prompt_cache_singleton.metrics.hits})print(fCache misses:{prompt_cache_singleton.metrics.misses})print(fHit rate:{prompt_cache_singleton.metrics.hit_rate:.1%})五、把 Prompt 同步到 GitHubWebhook 中间服务如果你希望“Prompt 变更 Git 一次提交 触发 CI/CD”可以用 Webhook 一个中间 HTTP 服务完成。官方推荐的做法是在 LangSmith 配置 Webhook指向你的公网服务每当 Prompt commit 时LangSmith 会发送包含 prompt_id/name/commit_hash/manifest 的 JSON。中间服务示例用 FastAPI做接收 POST解析 manifest用 GitHub PAT 调 GitHub API在指定仓库/路径/分支写入 manifest 文件。这样 Prompt 的每一次变更就变成了 Git 的一条提交可以走 CI/CD跑评测、自动部署等。六、一个实战的最小闭环建议你照着走一遍在 LangSmith Playground 写一个初始 Prompt模板 变量 模型并保存。在 Prompt 详情页打一个 staging 标签再 Promote 到 Production。在 Python 代码里用client.pull_prompt(xxx:production)拉取跑业务需要时指定某个 commit hash 测试。迭代时回到 Playground 或 Studio 改 Prompt → 保存 → 形成新 commit → 打新标签/推进环境 → 代码无需改动即可拿到新版本。如果要进 Git配置 Webhook FastAPI 中间件把每次 Prompt commit 写入 GitHub 仓库。如果你愿意告诉我你现在的技术栈LangGraphFastAPI/Django是否必须进 Git我可以帮你写一份更贴合的“Prompt 管理规范 示例代码”。