Learning resourcesReviewed · GitHub

GitHub Copilot SDK: Session lifecycle hooks

This GitHub Copilot SDK guide is for developers adding session setup and cleanup, but finishing a prompt does not end a standalone session. It documents start and end callbacks, context injection, configuration overrides, resource cleanup and an agent-stop hook that can request another turn.

Researched 24 Nov 2026

In short.

This GitHub Copilot SDK guide helps developers manage session setup and cleanup, but session-end callbacks require the session to end and may be missed after a process crash. Its central distinction is between a session ending and an agent finishing a turn: onSessionEnd supports session-wide cleanup, while onAgentStop can request continued work within an active session. The page provides signatures, field tables and examples, but leaves installation, SDK compatibility, pricing and licensing unspecified. The examples were not executed.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage includes the complete selected page at [https://docs.github.com/api/article/body?pathname=/en/copilot/how-tos/copilot-sdk/hooks/session-lifecycle], including all language tabs, examples, lifecycle cautions and the agent-stop section. The linked Use hooks, Error handling hook and Debugging guide pages are excluded. No examples were executed, and installation, version compatibility, runtime guarantees, pricing and licensing were not independently established.

The overview

Identity

Session lifecycle hooks is a GitHub Docs guide to lifecycle callbacks in the GitHub Copilot SDK. Its original address is [https://docs.github.com/api/article/body?pathname=/en/copilot/how-tos/copilot-sdk/hooks/session-lifecycle].

It covers onSessionStart, onSessionEnd and the separate agent-stop hook. It is documentation for implementing these features, rather than a downloadable integration or complete application.

Assessment

The guide is useful when a Copilot SDK application needs project context at startup, session metrics, temporary-resource cleanup or saved state for resuming work. Its field tables explain what handlers receive and return, while the examples show how application code can connect those events to storage and logging.

It provides a focused implementation reference. Several examples rely on helper functions whose implementations are absent, and the page does not establish runtime reliability, performance or compatibility with a particular SDK release.

Users

The intended readers are developers embedding Copilot SDK sessions in applications. A beginner can use the guide to understand when setup and cleanup happen; implementation requires familiarity with callbacks, asynchronous functions and application state.

The page provides start and end handler signatures for TypeScript, Python, Go, .NET and Java. Worked examples concentrate on TypeScript and Python. The agent-stop naming table also includes Rust, without providing Rust start or end handler signatures.

Capabilities

A start handler can supply additionalContext and modifiedConfig. Examples use it to describe a project, load user preferences and restore previously saved context. An end handler can return suppressOutput, cleanupActions and sessionSummary; examples also perform cleanup, logging and state saving directly inside the callback.

Metrics and persistence are application responsibilities in these examples: recordMetrics, loadSessionState and saveSessionState are helper calls, not implementations supplied by the guide. The agent-stop hook adds a separate ability to request another agent turn.

Using it

Requirements

The examples assume an existing SDK client and application code capable of creating a session and registering handlers. Project detection, preferences, persistence, metrics storage and file operations require supporting implementations where used.

The page does not specify package installation, authentication, required Copilot access, runtime versions or a minimum SDK version. The Python examples configure PermissionHandler.approve_all; whether that permission policy suits a particular application is not established.

Mechanism

onSessionStart runs for a new or resumed session. Its input includes timestamp, cwd, source and an optional initialPrompt; source can be startup, resume or new. Examples obtain the session identifier from the invocation argument and return additional context or no output.

onSessionEnd receives timestamp, cwd, reason and optional finalMessage and error fields. Documented reasons are complete, error, abort, timeout and user_exit. A standalone session remains active between prompts; disconnecting or disposing it triggers its end hook.

The agent-stop hook runs when the top-level agent naturally finishes a turn. Returning no output permits stopping; returning a block decision with a reason enqueues another user message and continues the agent. stopHookActive identifies a continuation already forced by an earlier block, and the runtime also limits consecutive blocks.

Setup

The TypeScript examples register onSessionStart and onSessionEnd inside the hooks object passed to client.createSession. Python uses on_session_start and on_session_end entries in the hooks dictionary passed to client.create_session.

To adapt a snippet, supply its missing helpers, choose the application's permission handler and arrange explicit session disconnect or disposal. Agent-stop registration names vary by language; the page lists those names but does not provide a complete setup example for every language.

Example

The metrics example stores each session's start timestamp in a map or dictionary keyed by session ID. Its end handler subtracts that timestamp from the end timestamp, passes the duration and end reason to recordMetrics, then removes the stored entry.

This illustrates lifecycle bookkeeping, but recordMetrics is not defined and the example does not demonstrate persistent storage or recovery after a crash. The page labels timestamps as Unix timestamps without explicitly specifying their unit; its separate summary example divides the elapsed value by 1000 to display seconds. Confirm the SDK's unit before interpreting durations.

Assessment & evidence

Limitations

The page explicitly warns that onSessionEnd might not run after a process crash. End-hook cleanup therefore cannot establish guaranteed recovery. It recommends idempotent cleanup, handling every end reason, keeping startup handlers fast and storing minimal state.

Valid modifiedConfig keys, cleanupActions syntax and execution semantics, callback failure behavior and the numerical limit on consecutive agent-stop blocks are not specified. Equivalent behavior across SDK versions and languages is not established.

Reasoning

The documented lifecycle distinction determines which hook fits a task. Because a session can span multiple prompts, onSessionEnd suits session-wide state and resource management. onAgentStop suits a decision about whether a completed turn should lead to more agent work.

A practical implication is that per-turn validation should not depend on a session-end callback. Likewise, issuing repeated block decisions without accounting for stopHookActive can request unnecessary continuations; the runtime cap provides an additional boundary whose size is unspecified.

Alternatives

Within this guide, onAgentStop is the relevant option for turn-level continuation, while onSessionEnd handles the end of the whole session. These hooks address different lifecycle events and are not interchangeable.

The page does not compare other SDKs, agent frameworks or external lifecycle-management tools. Their relative suitability, costs and reliability are not assessed.

Evidence

The evidence is the complete GitHub Docs page at [https://docs.github.com/api/article/body?pathname=/en/copilot/how-tos/copilot-sdk/hooks/session-lifecycle]. It contains handler signatures, input and output tables, language-specific names, examples and operational cautions.

The page provides documentation claims and illustrative code, without benchmarks or demonstrated execution results. The examples were not executed, so successful integration, performance and failure recovery remain unverified.

Access & freshness

Costs

The page provides no SDK pricing, required subscription price, model usage rates or hook-specific fees. Total operating cost is unknown.

It also provides no measurements of callback overhead, storage consumption or the usage associated with agent-stop continuations.

Rights

The page does not state licensing or redistribution terms for its text or code examples. A specific reuse license cannot be established from this evidence.

The permissions and terms governing use of the SDK or underlying service are also not described on this page.

Freshness

The documentation snapshot was captured on 24 November 2026 at 09:32:13.750 UTC. The page supplies no publication date, last-updated date, SDK version or compatibility range.

Capture time identifies the snapshot's age, but does not establish when these APIs changed or which installed SDK releases implement every documented field.

Actions

Before adoption, confirm the hook types against the intended SDK release and establish installation, authentication, pricing and reuse terms. Implement the helper functions needed by the chosen example and choose permissions appropriate to the application's required access.

Validate new and resumed sessions, explicit disconnect or disposal, applicable end reasons and bounded agent-stop continuation in your own environment. Check cleanup behavior when resources are already absent, and arrange recovery for cases where a crash prevents the end hook from running.

Evidence for this report

selected-page.txtselected · checked 2026-11-24