Liking cljdoc? Tell your friends :D

Context clearing and terminal tools

Use session.history.clearContext when a host needs to replace the current conversation context without replacing the session. Typical uses include handoffs and host-managed context lifecycle policies.

Context clearing is different from creating a new session: it preserves the session identity, system and developer messages, configuration, and event log while removing the model-facing conversation.

important

clearContext is a tool-handler primitive. The runtime rejects calls made without a tool call in flight, calls with an empty seed prompt, and calls on remote sessions.

Define a context-clearing tool

A successful context-clearing tool should be terminal. Otherwise, the agent loop may make another model call against the newly cleared window before starting the seeded turn.

import { approveAll, CopilotClient, defineTool } from "copilot-sdk-supercharged";
import type { CopilotSession } from "copilot-sdk-supercharged";
import { z } from "zod";

const client = new CopilotClient({ mode: "coding-agent" });
let session: CopilotSession;

session = await client.createSession({
    permissions: {
        onRequest: approveAll,
    },
    tools: {
        definitions: [
            defineTool("clear_context", {
                description: "Clear the conversation and start a fresh context window",
                parameters: z.object({ prompt: z.string() }),
                isTerminal: true,
                defer: "never",
                handler: async ({ prompt }) => {
                    const { messagesCleared } = await session.rpc.history.clearContext({ prompt });
                    return `Cleared ${messagesCleared} messages.`;
                },
            }),
        ],
    },
});

The required prompt becomes the first user message in the fresh context. A successful clear emits session.context_cleared with the number of removed messages and the initial message.

Terminal-tool behavior

isTerminal ends the current agent turn only when the tool succeeds. A failure, denial, timeout, or input-validation error remains visible to the model so it can recover or retry.

The option follows each language's naming conventions:

SDKTool option
Node.jsisTerminal
Pythonis_terminal
GoIsTerminal
.NETCopilotToolOptions.IsTerminal
JavaToolDefinition.isTerminal(true) or @CopilotTool(isTerminal = true)
Rustwith_is_terminal(true)

Use terminality only for tools whose successful completion should end the turn. Ordinary tools should leave it unset.

Can you improve this documentation? These fine people already did:
Jeremiah Isaacson, copilot-sdk-mirror-writer[bot], copilot-cli-release-app[bot] & examon
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close