The session tool allows for complex workflow orchestration using four primary modes: message, new, compact, and fork. You can optionally specify an agent parameter to switch the active agent (e.g., 'plan', 'build', or a custom agent name).
Modes
- COLLABORATE (
mode: "message"): Enables turn-based collaboration within the same conversation. Use this to pass a task from one agent to another (e.g., from a planner to a builder) without leaving the current thread. - HANDOFF (
mode: "new"): Creates a completely fresh session. This is ideal for phase transitions (e.g., moving from Research to Implementation) where you want to avoid carrying over previous context baggage. - COMPRESS (
mode: "compact"): Manually triggers session compaction to optimize token usage. It allows you to include a message and optionally hand off to a different agent after the history is compressed. - PARALLELIZE (
mode: "fork"): Branches the current session into an independent fork. This allows you to explore multiple implementation approaches simultaneously from the same starting point.
Available Agents
Agents are discovered from your filesystem (~/.config/opencode/agent/ or project-local .opencode/agent/). Built-in agents include:
build: General-purpose implementation agent.plan: Strategic planning agent.
Custom agents can be added as .md files with mode: "primary" or mode: "all" in their YAML frontmatter.
// COLLABORATE: Multi-agent problem solving
session({
mode: "message",
agent: "plan",
text: "Should we use microservices here?"
})
// HANDOFF: Clean phase transition
session({
mode: "new",
agent: "researcher",
text: "Research best practices for API design"
})
// COMPRESS: Long conversation with handoff
session({
mode: "compact",
agent: "plan",
text: "Continue architecture review"
})
// PARALLELIZE: Try multiple approaches
session({
mode: "fork",
agent: "build",
text: "Implement using Redux"
})
session({
mode: "fork",
agent: "build",
text: "Implement using Context API"
})