Repository navigation
Description
Feature hasn't been suggested before.
- I have verified this feature I'm about to request hasn't been suggested before.
Describe the enhancement you want to request
When a user switches between primary agents during a session, the conversation history sent to the model should include metadata indicating which agent handled each prior message. Currently, agents receive a flat transcript with no agent attribution — they cannot determine which agent produced which response.
Motivation: Retrospective agent for double-loop learning
I maintain a Retrospective agent that reviews completed sessions and suggests improvements to AGENTS.md and agent definition files. This is a form of double-loop learning — not just completing tasks, but improving the system that shapes how tasks are completed.
The Retrospective agent works by reading the conversation context to understand what happened during the session. It looks for:
- Repeated corrections — Did the user have to correct an agent multiple times? That's a signal to improve that agent's instructions.
- Wrong agent for the job — Was an agent used for a task outside its intended scope?
- Permission friction — Were there permission prompts that were always approved or denied?
- Convention violations — Did an agent violate project conventions?
Without per-message agent attribution, the Retrospective agent can only infer which agent handled each turn from context clues (tone, tool usage, the user explicitly saying "I used the wrong agent"). This is unreliable and limits the quality of feedback.
Concrete example
In a recent session, the user started with the Project Manager agent to evaluate an architecture decision (App Runner vs Lambda for web hosting). Partway through, they realized the PM agent wasn't the right fit and switched to a general agent. At the end, they switched to the Retrospective agent for review.
The Retrospective agent could see the content of the full session but had no way to know which messages came from the PM agent vs the general agent. The user happened to say "I pointed the wrong agent at that initially" — without that remark, the Retrospective agent would have had no signal to suggest improving agent routing for architecture decisions.
If agent identity were included in the message history, the Retrospective agent could have directly observed: "Messages 1-3 were handled by project-manager, messages 4-12 by default, messages 13+ by retrospective" — and given precise feedback like "The project-manager agent was used for an architecture evaluation task outside its scope; consider documenting when to use which agent."
Proposed approach
Include the agent name (matching the filename in .opencode/agents/) as metadata on each assistant message in the conversation context. For example:
[agent: project-manager]
<assistant message content>
[agent: default]
<assistant message content>
The exact format is up to the OpenCode team — it could be a prefix, a structured field, or a system message injected at switch points. The key requirement is that downstream agents can determine which agent produced each prior response.
Relationship to existing issues
- [Bug] Primary agents lack identity awareness when user switches agents - cannot determine which agent is active #7492 addresses agents not knowing their own identity. This request is complementary — it's about agents knowing which other agents handled prior messages.
- [FEATURE]:Agent Switch During a Session in Desktop shall be "Printed" #11801 requests agent switches be visible in the UI. This request extends that concept to the model-facing conversation context.
OpenCode version
1.1.8
Activity
github-actions commentedon Feb 24, 2026
This issue doesn't fully meet our contributing guidelines.
What needs to be fixed:
[FEATURE]:(e.g.[FEATURE]: Include per-message agent identity in conversation context sent to the model)Please edit this issue to address the above within 2 hours, or it will be automatically closed.
If you believe this was flagged incorrectly, please let a maintainer know.
[-]Include per-message agent identity in conversation context sent to the model[/-][+][FEATURE] Include per-message agent identity in conversation context sent to the model[/+][-][FEATURE] Include per-message agent identity in conversation context sent to the model[/-][+][FEATURE]: Include per-message agent identity in conversation context sent to the model[/+]github-actions commentedon Feb 24, 2026
Thanks for updating the issue! It now meets the contributing guidelines — the feature request is clearly structured with a real use case, motivation, and a concrete example. Appreciate the detail!
gotgenes commentedon Mar 3, 2026
I ended up publishing a plugin that addresses this as a workaround while native support is developed:
@gotgenes/opencode-agent-identity— source at https://github.com/gotgenes/opencode-agent-identityIt uses the
experimental.chat.messages.transformhook to prepend a[agent: X]header to each assistant message's first text part before it reaches the LLM — so every agent (including Retrospective) sees attributed messages like:The mutation is ephemeral — it only affects what the model sees per call, without altering the persisted database.
Installation
Add to your
opencode.json:{ "plugin": ["@gotgenes/opencode-agent-identity"] }The package also includes
AgentSelfIdentityPlugin(for #7492), so you get both behaviors in one install.I've verified this works with my Retrospective agent — it can now reliably attribute each turn to the correct agent.
gotgenes commentedon Mar 5, 2026
Update:
@gotgenes/opencode-agent-identityv2 switches from inline[agent: X]tags to a tool-based approach for agent attribution.What changed and why
v1 injected
[agent: X]headers directly into assistant message text via theexperimental.chat.messages.transformhook. This worked for attribution but caused two problems in multi-agent sessions:[agent: X]pattern and started generating the tags themselves, creating a feedback loop that reinforced the wrong identity.v2 replaces this with an
agent_attributiontool that agents can call on demand. The tool queries the session's message history via the OpenCode SDK and returns a numbered list mapping each message to its role and agent:This keeps the conversation history clean — no mutations to message text — and agents only see attribution data when they explicitly request it.
Integration
Agents that need attribution (e.g., a Retrospective agent) just need a mention in their system prompt:
The tool is registered automatically when the plugin is installed — no changes to
opencode.jsonneeded beyond the existing plugin entry.CarloWood commentedon Apr 9, 2026
@gotgenes Are you only OpenCode's discord? If so, what is your handle?
gotgenes commentedon Apr 10, 2026
@CarloWood I am not yet on OpenCode's Discord. But I do respond to email, or to GitHub Issues.
gotgenes commentedon Apr 24, 2026
I have also created @gotgenes/opencode-session-context for allowing other agents and subagents to access message history from arbitrary sessions, which, again, is useful for letting agents and subagents assess behavior and performance.
github-actions commentedon Jul 4, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.