# Session Start Hook

> The Session Start Hook is an agent hook that runs when a new agent session begins.

## How do I prepare the environment for an AI agent session?

The hook that runs at the start of a session, for example to install dependencies, load environment variables or add project status to the context, so the agent starts with a ready environment.

- Topic: Agent Customization
- Type: concept

## Example

Wrong:

```bash
#!/bin/bash
rm -rf node_modules
npm install
```

Right:

```bash
#!/bin/bash
set -euo pipefail
if [ ! -d node_modules ]; then
  npm ci --silent || { echo "npm ci failed" >&2; exit 1; }
fi
echo "Branch: $(git branch --show-current)"
```

Installing only when needed makes the hook idempotent and fast, and errors are printed instead of hidden.

## Key points

- The hook is idempotent: running it twice does not break anything.
- The hook finishes quickly and prints errors instead of failing silently.

## Interview questions

### What is a session start hook useful for?

Installing dependencies, loading environment variables and adding a short project status, such as the branch or open tasks, to the agent's context.

### Does a session start hook run on resumed sessions?

It depends on the tool. Some run it on new, resumed and cleared sessions and pass the reason to the hook.

### How is a session start hook different from AGENTS.md?

AGENTS.md is static text. A session start hook runs commands, so it can prepare the environment and add facts that change between sessions.

## Related cards

- [Agent Hooks](https://wonderium.dev/ai/agent-hooks.md)
- [Prompt Submit Hook](https://wonderium.dev/ai/prompt-submit-hook.md)
- [Background Agent](https://wonderium.dev/ai/background-agent.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/session-start-hook
- Last updated: 2026-11-25
