🪝 VS Code Agent Hooks: Give Copilot Context Before the First Message
Sometimes we need the AI and the human agent to have different context depending on the situation: the type of project, the technology…
🪝 VS Code Agent Hooks: Give Copilot Context Before the First Message
Sometimes we need the AI and the human agent to have different context depending on the situation: the type of project, the technology we’re using, the level of the team we’re working with. Starting a session in a client project is not the same as starting a personal one, and the agent has no way of knowing this unless you tell it every time, in every session.
Did you know you can automate that with your own script? Agent Hooks allow you to automatically run code at specific points in the agent lifecycle, before the user types anything.
An agent hook is a command or script that VS Code executes at a specific point during an agent session. VS Code sends the event data in JSON format (via ), and the script can respond with JSON (via ) to influence the agent’s behavior. There are eight events available in the agent lifecycle:
VS Code automatically loads all .json files located in .github/hooks/. No additional configuration is required.
In this post I’ll show you how to use two hooks: SessionStart to perform onboarding at the beginning of each session, and PreCompact so that the agent doesn’t lose context when VS Code compacts the conversation. The specific goal: when a new session opens, the agent automatically asks three questions (technology, experience level, and project type) and saves the answers. These answers are then used to load the appropriate behavioral profile for that session. And if the conversation is later condensed, the second hook retrieves that context without losing it. The folder structure is as follows:
How do we make it work?: Create .github/hooks/custom-hooks.json
- type: Defines that the hook runs an external command (“command” is the only type available right now).
- windows: The command executed on Windows, in this case, a PowerShell script.
- command: Fallback when windows is not defined; VS Code uses this on Linux/macOS.
- cwd: Working directory from which the script runs, relative to the repository root.
- env: Additional environment variables the script can read (e.g., $env:HOOK_AUTHOR).
- timeout: Seconds VS Code waits for a response before continuing without it.
The “ SessionStart” hook fires only once when a new session begins. The “ log-session.ps1 “ script instructs the agent to ask the user three questions (one at a time).
This is what an entry in trace-log.jsonl looks like, which is the complete log with the input received by the hook when it fires:
The “ additionalContext” is already sufficient for the AI to understand the session context. Within that same text, we instruct the agent to serialize the responses into a JSONL file. Without this step, “ PreCompact “ would have nothing to read if the conversation is compacted. Once saved, the agent activates the corresponding profile with its own behavioral instructions.
The session-log.jsonl file saves only the timestamp, session_id, and author, one line per session, without the full payload. Useful for auditing without opening the trace.
The onboarding result is saved in session-answers.json; this is what PreCompact will read to reinject the context if the session is compacted.
When a conversation is very long, VS Code automatically compacts the context. What’s the problem? The agent might miss the onboarding responses and start behaving as if it knows nothing. The “ PreCompact “ hook fires just before this compaction occurs. Therefore, it’s an opportunity to reinject context before it’s lost.
The script “ reinject-profile.ps1 “ reads the responses from the current session from the JSONL and loads the active profile, in addition to returning and injecting the context with the responses and the complete profile.
Hooks don’t replace instructions or custom prompts; instead, they provide dynamic runtime context. If you want to use this example directly, I’ve published the entire “.github/hooks/” folder here.: Blog/GDRGDev_VSCHooks
I hope this information helps you.
Más información / More information:
Originally published at http://gerardorenteria.blog on May 3, 2026.
메타데이터
- post_id
- a7b57cb9f4f8
- slug
- vs-code-agent-hooks-give-copilot-context-before-the-first-message-a7b57cb9f4f8
- url
- https://medium.com/@gdrenteria/vs-code-agent-hooks-give-copilot-context-before-the-first-message-a7b57cb9f4f8
- canonical_url
- https://medium.com/@gdrenteria/vs-code-agent-hooks-give-copilot-context-before-the-first-message-a7b57cb9f4f8
- author_url
- https://medium.com/@gdrenteria
- status
- ok
- fetched_at
- 2026-08-17 15:57:22