Skip to content

[FEATURE]: Include per-message agent identity in conversation context sent to the model #14930

Closed as not planned

Description

@gotgenes

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

OpenCode version

1.1.8

Activity

  1. github-actions commented on Feb 24, 2026

    @github-actions
    Contributor

    This issue doesn't fully meet our contributing guidelines.

    What needs to be fixed:

    • The title of a Feature Request should start with [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.

  2. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Feb 24, 2026
  3. changed the title [-]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[/+] on Feb 24, 2026
  4. changed the title [-][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[/+] on Feb 24, 2026
  5. github-actions commented on Feb 24, 2026

    @github-actions
    Contributor

    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!

  6. gotgenes commented on Mar 3, 2026

    @gotgenes
    Author

    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-identity

    It uses the experimental.chat.messages.transform hook 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:

    [agent: plan]
    Here is the analysis you requested...
    

    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.

  7. gotgenes commented on Mar 5, 2026

    @gotgenes
    Author

    Update: @gotgenes/opencode-agent-identity v2 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 the experimental.chat.messages.transform hook. This worked for attribution but caused two problems in multi-agent sessions:

    1. Identity confusion — accumulated tags from a previous agent overwhelmed the system prompt identity, causing the model to identify as the wrong agent.
    2. Self-authoring — models learned the [agent: X] pattern and started generating the tags themselves, creating a feedback loop that reinforced the wrong identity.

    v2 replaces this with an agent_attribution tool 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:

    1. user (project-manager)
    2. assistant (project-manager)
    3. user (default)
    4. assistant (default)
    5. user (retrospective)
    6. assistant (retrospective)
    

    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:

    ## Multi-agent attribution
    
    This session may involve multiple agents. To determine which agent authored
    each message, call the `agent_attribution` tool. It returns a numbered list
    mapping each message to its role and agent name.

    The tool is registered automatically when the plugin is installed — no changes to opencode.json needed beyond the existing plugin entry.

  8. CarloWood commented on Apr 9, 2026

    @CarloWood
    Contributor

    @gotgenes Are you only OpenCode's discord? If so, what is your handle?

  9. gotgenes commented on Apr 10, 2026

    @gotgenes
    Author

    @CarloWood I am not yet on OpenCode's Discord. But I do respond to email, or to GitHub Issues.

  10. gotgenes commented on Apr 24, 2026

    @gotgenes
    Author

    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.

  11. assigned and unassigned on Apr 26, 2026
  12. removed
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  13. github-actions commented on Jul 4, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions