Session
$.session lets a mod read the running session as plain data, compact it, and send messages to other agents or sessions.
Its events tell a mod when a session starts, ends, compacts, or gets a message.
Return { skip }, { consumed }, or { deny } from a hook to stop an action.
Official pages: Use the Mods API, Mods Reference. Source: the program (types-2.1.304.d.ts).
In a "Where it can be used" cell, "Run time" means in a hook, in a closure that a hook drew, or in a timer callback. It does not mean inside register.
Example
This hook shows a toast when a session starts.
on('session.start', async ($, e, next) => {
$.ui.toast('Session started')
return next(e)
})
Members
| Item | Status | What it does | How it works | Where it can be used |
|---|---|---|---|---|
$. | Documented | Returns the transcript so far, one Session for each user or assistant message. Progress rows, $. lines, and notices are not messages. | Five call forms. No argument gives the main conversation. { agent gives an agent's messages. { as: gives raw API messages. At most 4096 entries. | Run time. |
$. | Documented | Returns the directory the session runs in, absolute. | () => Promise<string>. A timer that outlives its hook reads the session's directory. Keep the value you read inside the hook if you need the hook's own (a subagent's). | Run time. |
$. | Documented | Returns the project root of the session, absolute: where it started, or where /, a host's directory change, or a worktree move took it. | () => Promise<string>. A shell cd does not move it. Nested instruction files are read only beneath it. | Run time. |
$. | Documented | Returns the main loop's model, as / shows it. | () => Promise<string>. | Run time. |
$. | Documented | Returns how many prompts the user has sent this session. | () => Promise<number>. | Run time. |
$. | Documented | Returns the session id (the transcript file's name). | () => Promise<string>. | Run time. |
$. | Documented | Returns the git repository the session runs in, read from the working copy on each call. null when the directory is not in one. | () => Promise<Session. | Run time. |
$. | Documented | Returns every surface the session draws on, each once: terminal first, then remote ones in the order they attached. | () => Promise<readonly Render. Never rejects. Empty in a plain - run. | Run time. |
⛔ $. | Documented | Returns the first of $., or null where nothing draws. | () => Promise<Render. Deprecated. Use surfaces(), because a session may draw on several surfaces. | Run time. |
$. | Documented | Returns when the session began, the context window fill, the rate-limit windows, and the cost, as the status line has them. | (args?) => Promise<Session. A plain call costs nothing. breakdown: uses the token-count API. "summary" estimates locally. | Run time. |
$. | Documented | Returns the version of the engine, the release it is built from, and when it was built. | () => Promise<Session: version, base (absent unless the version is a release), built. Works in every mode. | Run time. |
$. | Documented | Compacts the conversation. It is the event session. with trigger plugin, the same call / makes. | (input?) => Promise<Session. Runs the hooks, then core. Resolves { skip } if a hook vetoed it. Rejects while a turn runs. | Between turns. |
$. | Documented | Sends a plain-text message to another agent or session. It is the event session., the same delivery as the model's SendMessage tool. | (input) => Promise<Session. to is a name, an agent id, { session, or { agent. Resolves once queued. | Run time. |
$. | Documented | Appends a row to a conversation of the session. It is the event session., the engine's own call for every row it keeps. | (args) => Promise<Session. Adds a user-role row of text blocks. Resolves { deny: if a plugin above refused it. | Run time. |
$. | Documented | Returns how the session is signed in, to a mod that the organization's managed settings install. Any other mod gets { deny }. No request is made. | () => Promise<Session. Full account data only where managed force lists the organization. Otherwise only kind. | Mods that managed settings install. |
$. | Documented | Holds the session's Anthropic credential on the host and returns an opaque handle with its kind, for example { handle:. The secret never reaches the mod. Returns null when the session has no first-party credential (a third-party provider, a cloud gateway, no login or key). | () => Promise<Session. Spend the handle with $.. The engine sets the credential header, and only for a first-party host. A handle belongs to the mod that minted it, and a mod keeps at most 4 live handles (a fifth evicts the oldest). An unknown or foreign handle makes $. throw. kind is bearer or api-. No managed-settings gate, unlike $.. | Run time. |
Engine Events
| Item | Status | What it does | How it works | Where it can be used |
|---|---|---|---|---|
session. | Documented | Fires once for each loaded plugin per process, before the first prompt, then once for each fresh load of a plugin (never on /). next(e) is { cwd }. | Input Session. Result Session. The first run is awaited, so a $. in it is listed by turn one. | on('session.. In tests: $. on the test engine (not on a hook's $). |
session. | Documented | Fires when a delivery reaches the session, before it is queued: a relay's event, a peer's message, a Remote Control prompt, room events. | Input Session. Result { text } or { consumed }. Return { consumed: to stop the message from reaching the model. | on('session.. In tests: $. on the test engine (not on a hook's $). |
session. | Documented | Fires once for each row a conversation of this session keeps (a prompt, a response block, a tool result, a notice), before it is stored. | Input Session. Result { message, uuid }. next({ ... rewrites content before it is stored. | on('session.. Also $.. |
session. | Documented | Fires when a plain-text message is about to leave this conversation for another agent or session. | Input Session. Result { is. Rewrite text or to with next. Return { is to refuse. | on('session.. Also $.. |
session. | Documented | Fires when the conversation is about to compact (/, the threshold, a plugin, or ahead of time). next(e) resolves { messages }. | Input Session. Result { messages } or { skip }. { skip: leaves the conversation as it is. | on('session.. Also $.. |
session. | Documented | Fires when a remote client joins the roster of attached surfaces. | Input Session. Result { client. A hook observes a client that joins. $. reads the roster. | on('session.. |
session. | Documented | Fires when a client leaves the roster: it detached, or the session ended with it attached (e.). | Input Session. Result { client. With reason end it runs inside the short bound of session.. | on('session.. |
session. | Documented | Fires when the engine measures the session and a unit moved: after each main-thread turn, and when a rate-limit window moves a whole point. | Input Session. Result { changed }. It pushes the figures of $.. Calls arrive one at a time. | on('session.. |
session. | Documented | Fires once when the session ends (exit, /, resume, logout, signal, a finished - run), after its Session settings hooks. e. says which. | Input Session. Result { session. The whole chain shares one short time bound that next. reads. | on('session.. |
Events Raised by Session Calls
For each call, a hook above the caller returns next(e), { deny: reason }, or { value }.
| Item | Status | The argument | Input and value |
|---|---|---|---|
session., session., session., session. | Documented | The matching $. call. | Input No. Value string. |
session. | Documented | $. | Input No. Value number. |
session. | Documented | $.: {} for the main conversation, { agent for an agent, as for the form. | Input Session. Value Session. |
session. | Documented | $. | Input No. Value Session. |
⛔ session. | Documented | $.. Deprecated: hook session. instead. | Input No. Value Render. |
session. | Documented | $. | Input No. Value readonly Render. |
session. | Documented | $. | Input No. Value Session. |
session. | Documented | $. | Input No. Value Session: { handle, kind }, or null when the session holds no first-party credential. |
session. | Documented | $. | Input Session. Value Session. |
session. | Documented | $. | Input No. Value Session. |
The $.http.fetch handler runs a pre-check before it resolves the handle, when auth is set. Its only refusal: if CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC is set, the request throws an error that ends "the session's credential is withheld: nonessential network traffic is disabled for this session".
When no credential resolves, $.session.authorize logs $.session.authorize (<plugin>): no first-party credential (<reason>) and returns null. It does the same if a workload-identity token is minted for another host, or if the auth mode changes during the call.
Related Pages
- Mod API Overview
- Agent
- Session Types (context, messages, usage, and send types)