Allow agent to clear context between tasks and continue with previous session ID

Open 💬 6 comments Opened May 18, 2026 by cowwoc
💡 Likely answer: A maintainer (github-actions[bot], contributor) responded on this thread — see the highlighted reply below.

What variant of Codex are you using?

CLI

What feature would you like to see?

Add a first-class agent capability for moving cleanly from one task to the next by intentionally clearing the current context and starting a fresh continuation prompt seeded with the previous session ID.

This is a happy-path workflow, not a failure recovery path. The goal is to let the agent finish or pause one unit of work, drop the now-stale working context for that task, and begin the next unit of work with a clean context while preserving an audit/reference link back to the prior session.

A common workflow would be:

  1. The agent finishes the current issue or reaches a natural task boundary.
  2. The agent invokes a supported control operation to clear/reset the active context.
  3. Codex starts a fresh context whose initial state includes the previous session/thread ID.
  4. Codex executes a new prompt supplied by the agent, for example:
Previous session ID: X.
Work on the next issue.

In a project that uses an issue tracker or local task workflow, the fresh prompt can trigger the normal “work on the next issue” flow without carrying over all the old file reads, command output, intermediate reasoning, and task-specific details from the previous issue.

A rough API shape could be something like:

{
  "action": "clear_context_and_continue",
  "previousSessionId": "<current-session-id>",
  "prompt": "Previous session ID: <current-session-id>. Work on the next issue."
}

The important part is not the exact API shape, but that this is a real Codex control path. Sending /clear, /compact, or similar text back to the model should not be required.

Why this would help

Long-running project work often consists of many discrete tasks. Context from the previous task can be useful for auditability, but it is usually not useful enough to keep in the active model context once the next task begins.

A task-boundary context clear would let agents run cleaner multi-issue workflows:

  • finish one issue and start the next with less stale context
  • preserve a durable reference to the prior session for traceability
  • avoid carrying previous-task implementation details into unrelated work
  • keep issue-tracker workflows moving without requiring the user to manually start a new chat
  • make the transition explicit and understandable to the user

Desired semantics

  • The operation should be agent-accessible only through a guarded control/tool path.
  • The new context should contain the previous session/thread ID in a machine-readable way.
  • The agent should provide the first prompt to run in the new context.
  • Codex should make the transition visible to the user so it is clear that a task-boundary context clear happened.
  • The previous session should remain inspectable/resumable for audit and recovery.
  • The old context should not automatically be copied wholesale into the new context.
  • The feature should support normal issue-tracker prompts such as “work on the next issue” after the clear.
  • Safeguards should prevent accidental loops or repeated clears without user-visible progress.

Related issues

  • #21777 asks to expose compaction to the agent.
  • #21673 asks for a compact handoff command with copyable context.
  • #23153 asks for hooks to request real compaction before continuing.

This request is related, but distinct: it asks for an agent-controlled task-transition path that clears active context, carries the previous session ID, and immediately executes a new prompt for the next task.

View original on GitHub ↗

6 Comments

github-actions[bot] contributor · 3 months ago

Potential duplicates detected. Please review them and close your issue if it is a duplicate.

  • #22641

Powered by Codex Action

etraut-openai contributor · 3 months ago

You can do this with subagents today. Ask the primary agent to create a subagent. It will have a clean context. You can ask the parent to seed it with anything you'd like. Subagents were designed to handle the type of workflow that you're describing.

I think we're unlikely to add the ability for an agent to start a new peer session. The reason we support subagents is because those are tracked as part of the "thread tree" that's rooted in the parent thread.

cowwoc · 3 months ago

@etraut-openai The problem with the subagent approach is the resulting UX. I want to clear-and-continue work on the visible agent without user intervention. If I spawn a new agent as you suggest then the user will have to manually switch focus, which is not much better than asking them to manually running /clear and starting work on the next issue.

nitecon · 2 months ago

I built agent-memory as seen here: https://github.com/nitecon/agent-memory. With the use of this having the ability to have the agent clear context between execution tasks would be ideal as the agent-memory contains the working context that the agents already update per the rules, which makes the entire system way more efficient, and this is also important for the main agent.

cowwoc · 2 months ago

@nitecon If you want this issue, vote for it.

nanopink · 1 day ago

+1. The workflow I need is:

Do Y. After Y is complete, wipe the active context and do Z.

Codex should finish Y, hard-reset the model context (not compact or summarize it), and start Z without another user interaction. The visible conversation, workspace/worktree, and CLI or App should remain unchanged; only the model context resets. Z should receive normal instructions and workspace state, but none of Y's transcript or tool output unless explicitly handed off.

A boundary marker should confirm the reset. Internally, retain Z outside the context being wiped, then inject it afterward. The existing experimental new_context primitive looks close.