implementing-agent-modes by posthog/posthog
npx skills add https://github.com/posthog/posthog --skill implementing-agent-modes使用以下步骤来规划或实现一个新的模式。模式是一种管理代理上下文并注入与产品、用例、JTBD 等相关的工具、提示和模式行为的方式。代理拥有 switch_mode 工具,允许其自身切换到另一个模式,这可能会改变工具、提示和可执行文件,同时保留当前上下文。一些先前创建的工具是上下文相关的,意味着它们被注入到前端的特定页面。模式改变了方法,并且总是在模式上下文中拥有工具。
探索 ee/hogai/core/agent_modes/presets 目录,检查是否已有与用户意图匹配的模式。如果你想创建一个新模式,你应该按 PostHog 产品(产品分析)、产品领域(SQL)或代理(插装代理)来界定其范围。
在 frontend/src/queries/schema/schema-assistant-messages.ts 中添加一个新的 AgentMode,并使用以下命令重新生成模式:
hogli build:schema
或者,你也可以使用这个命令:
pnpm run schema:build
一个模式通常至少应包含两样东西:
注意:只有当用户需要修改该模式的提示、行为或执行循环本身时,才应创建新的可执行文件。
相关的工具可能位于 或 中。有一组工具总是被注入到上下文中,例如 工具,但所有其他工具都应是特定于模式的。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
ee/hogai/toolsproducts/<product_name>/backend/max_toolsread_data在将工具添加到工具包之前,请确定这些工具是否有工具依赖项。如果存在依赖项(例如实验依赖于功能标志创建),请返回用户处,以确定他们是否希望将模式合并为一个。如果他们不想这样做,请确保稍后添加一个轨迹示例,清楚地解释模式切换和工具选择。
你还应该验证这些工具是否是后端优先的。如果工具应用前端更改而没有将适当的上下文传递回对话,你应该提出一种方法使其成为后端优先,以便代理拥有正确的上下文。
如果新模式包含新的 Django 模型,你应该审查 read_data、search 和 list_data 工具是否具有检索这些模型的功能。如果它们不支持这些模型,你应该使用或实现在 ee/hogai/context/... 中可用的上下文提供程序之一。
更新 AgentToolkit 以包含轨迹示例。这些应该是 JTBD 风格的示例,展示代理应如何使用可用工具完成典型任务。请参考产品分析预设。
更新 max-constants.tsx 以包含新工具,并将模式添加到模式选择器中。你可能还需要创建新的 UI 元素来显示来自工具的数据。
假设你更新了错误跟踪工具以列出问题。它过去是一个只更新过滤器的前端工具,但现在它输出错误跟踪问题。虽然代理拥有所需的上下文,但用户也需要以人类可读的方式查看问题。在这种情况下,你应该设计并实现一个新的组件来显示工具的输出。
所有新模式都必须受功能标志控制。示例:
@property
def mode_registry(self) -> dict[AgentMode, AgentModeDefinition]:
registry = dict(DEFAULT_CHAT_AGENT_MODE_REGISTRY)
if has_error_tracking_mode_feature_flag(self._team, self._user):
registry[AgentMode.ERROR_TRACKING] = error_tracking_agent
return registry
如果你创建了新工具,请确保正确地为它们设置功能标志:
你应该测试新工具、预设、可执行文件,并可选地实现评估。
每周安装量
161
仓库
GitHub 星标数
32.2K
首次出现
2026年1月22日
安全审计
安装于
codex156
opencode156
gemini-cli155
cursor153
claude-code152
github-copilot152
Use the steps below to plan or implement a new mode. A mode is a way to manage the context of the agent and inject tools, prompts, and mode-related behavior relevant to a product, use case, JTBD, etc. The agent has the switch_mode tool that allows it to switch itself to another mode, which might change tools, prompt, and executables, preserving the current context. Some previously created tools are contextual, meaning they're injected on particular pages of the frontend. The modes change the approach and always have tools in the mode context.
Explore the ee/hogai/core/agent_modes/presets directory and check if there are already modes that match the user's intent. If you want to create a new mode, you should scope it by a PostHog product (Product analytics), product area (SQL), or agent (Instrumentation agent).
Add a new AgentMode to frontend/src/queries/schema/schema-assistant-messages.ts and regenerate the schema using:
hogli build:schema
Alternatively, you may use this command:
pnpm run schema:build
A mode should typically contain at least two things:
Note: you should only create new executables if the user needs to modify the prompt, behavior of that mode, or the execution loop itself.
Relevant tools might be located in ee/hogai/tools or products/<product_name>/backend/max_tools. There is a set of tools that is always injected into the context, like the read_data tool, but all other tools should be specific to the mode.
Before adding a tool to the toolkit, determine if those tools have tool dependencies. If there are dependencies (like an experiment depends on feature flag creation), loop back to the user to determine whether they want to merge modes into a single one. If they don't want to do that, make sure that you later add a trajectory example clearly explaining mode switching and tool selection.
You should also verify that the tools are backend-first. If tools apply frontend changes without passing proper context back to the conversation, you should propose a way to make them backend-first so the agent has the right context.
If the new mode contains new Django models, you should review whether the read_data, search, and list_data tools have the functionality to retrieve the models. If they don't support these models, you should use or implement one of the context providers available in ee/hogai/context/....
Update the AgentToolkit to include trajectory examples. These should be JTBD-style examples showing how the agent should achieve typical tasks with the available tools. Check the Product analytics preset for reference.
Update max-constants.tsx to include new tools and add the mode to the mode selector. You might also need to create new UI elements for displaying data from the tools.
Say you've updated the Error tracking tool to list issues. It used to be a frontend tool that only updated filters, but now it outputs error tracking issues. While the agent has the context it needs, the user also needs to see the issues in a human-readable way. In this case, you should design and implement a new component to display the tool's output.
All new modes must be feature-flagged. Example:
@property
def mode_registry(self) -> dict[AgentMode, AgentModeDefinition]:
registry = dict(DEFAULT_CHAT_AGENT_MODE_REGISTRY)
if has_error_tracking_mode_feature_flag(self._team, self._user):
registry[AgentMode.ERROR_TRACKING] = error_tracking_agent
return registry
If you have created new tools, make sure you feature flag them correctly:
You should test new tools, presets, executables, and optionally implement evals.
Weekly Installs
161
Repository
GitHub Stars
32.2K
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex156
opencode156
gemini-cli155
cursor153
claude-code152
github-copilot152
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
152,900 周安装