Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PI Agent Context Management

Pi Agent Context Management

Context management is a core skill for using Pi Agent efficiently.

This chapter explains how to control the AI's behavior through environment files and how to manage conversation context.


Context file overview

Pi Agent automatically loads project instruction files at startup so the AI understands your project conventions, commands, and preferences.

These files tell the AI 'how to work in this project'.


AGENTS.md / CLAUDE.md

This is Pi Agent's most important context file; the loading order is:

  1. ~/.pi/agent/AGENTS.md - global instructions, effective for all projects
  2. AGENTS.md or CLAUDE.md in each parent directory traversed upward from the current directory
  3. AGENTS.md or CLAUDE.md in the current directory

The following is a complete example placed in AGENTS.md at the project root:

Example

Project instructions

Code Standards

All code uses TypeScript strict mode

Functions must have return type annotations

Use ESLint and Prettier to keep code style consistent

Workflow

Run after code changes

npm run check

Validation

Run before committing

npm test

Ensure tests pass

Do not run production database migrations directly locally

Security tips

Do not write API Keys or passwords into code

Use environment variables for sensitive configuration

Response style

Keep responses concise; don't over-explain obvious code

When a problem occurs, give the fix first, then explain the reason

After updating AGENTS.md, use the /reload command to hot-reload without restarting Pi Agent.

If you have both AGENTS.md and CLAUDE.md, Pi Agent loads AGENTS.md first.

If you previously used Claude Code, you can reuse your existing CLAUDE.md.

If AGENTS.override.md exists in a directory, it replaces that directory's AGENTS.md and CLAUDE.md for loading.

To disable context file loading, use the command-line option:



$ pi --no-context-files

$ pi -nc   # 短形式


SYSTEM.md

SYSTEM.md completely replaces Pi Agent's default system prompt; if you only want to append content, use APPEND_SYSTEM.md instead.

Both come in global and project levels, with paths and roles as follows:

file Level Purpose
~/.pi/agent/SYSTEM.md Global Replace the default system prompt
project root/.pi/SYSTEM.md Projects Replace the default system prompt
~/.pi/agent/APPEND_SYSTEM.md Global Appended after the default system prompt
project root/.pi/APPEND_SYSTEM.md Projects Appended after the default system prompt

In most cases you don't need to modify the system prompt.

AGENTS.md already covers the vast majority of customization needs.

SYSTEM.md suits scenarios requiring deep customization of AI behavior; improper changes may affect the AI's effectiveness.


Context compaction (Compaction)

When a conversation gets very long, the AI's context window gradually fills up.

Pi Agent solves this with context compaction—it automatically summarizes earlier conversation content into a brief summary, freeing up context space.

context compression loop: message appending, approaching the threshold, automatic summarization, folding early messages

Automatic compaction

By default, Pi Agent automatically triggers compaction when the context approaches the model limit.

Compaction behavior is controlled by the compaction configuration in ~/.pi/agent/settings.json:

Example

{

  "compaction": {

    "enabled": true,

    "reserveTokens": 16384,

    "keepRecentTokens": 20000

  }

}

Configuration Item Type Default value Description
compaction.enabled boolean true Whether to enable automatic compaction
compaction.reserveTokens number 16384 Number of tokens reserved for LLM responses
compaction.keepRecentTokens number 20000 Number of recent tokens kept uncompressed

The trigger condition is when context tokens exceed the available space after contextWindow minus reserveTokens.

The check happens after each tool execution completes and before the next user prompt is sent.

Manual compaction

You can also trigger compaction manually at any time:



/compact

When compacting, you can add custom instructions telling the AI what to focus on in the summary:



/compact 重点关注错误修复和 API 变更

Compaction is irreversible only for the current conversation context.

The compressed original messages remain intact in the session file; use /tree to jump back to the pre-compaction node to recover details.

If you want a complete backup before compaction, /clone is recommended.

/clone fully copies the current active branch to a new session file, suitable for pre-compaction backup.

/fork only starts from a certain historical user message; content after it is not carried into the new session.


Reasoning display

Models that support reasoning output think before answering.

You can control the display of the reasoning process with the following settings:

Settings Type Default value Description
hideThinkingBlock boolean false Hide the reasoning process, show only the final answer
showCacheMissNotices boolean false Show cache-miss hints and also token usage hints from compaction and branch summaries

Use Ctrl+T to toggle the reasoning display on/off at any time during a conversation.


Hot reload (/reload)

Whether you need to restart after changing a configuration depends on what you changed.

The table below summarizes how common changes take effect; items marked 'No' just need /reload:

Change Restart required Description
AGENTS.md or CLAUDE.md (context files) No Reloaded immediately after /reload
Extensions No Reloaded immediately after /reload
Skills No Reloaded immediately after /reload
Prompt templates No Reloaded immediately after /reload
Theme files No Reloaded immediately after /reload
Keyboard shortcut configuration No Reloaded immediately after /reload
Certain settings in ~/.pi/agent/settings.json Is Some configuration is read once at startup
Newly installed extension packages Is Package dependencies are resolved at startup
auth.json credentials Is Credentials are loaded at startup

Viewing context usage

The bottom status bar shows the current context token usage and cost in real time.

Use the /session command to see more detailed information:



/session

Example output:



Session file: ~/.pi/agent/sessions/--Users-runoob-projects-runoob-demo--/2026-08-30T09-15-04-000Z_019fbb5d-7a2e-7f31-b5c4-1a2b3c4d5e6f.jsonl

Session ID:   abc123-def456

Messages:     42

Tokens:       85,432

Cost:         $0.52

Current model: claude-sonnet-4-20250514