What this snippet demonstrates
AFK agents need system prompts (instructions) that tell the LLM how to behave. Rather than hardcoding instructions as inline strings, AFK provides a file-based prompt resolution system that loads prompts from a directory hierarchy. This keeps prompts version-controlled, editable by non-developers, and reusable across agents. The prompt loader resolves instructions through a deterministic precedence chain, supports Jinja2 templating for dynamic prompts, and caches compiled templates using stat-based invalidation for hot-reload during development.Resolution precedence
The prompt system resolves agent instructions through this priority chain:- Inline
instructions— If the agent has a non-emptyinstructionsstring, it is used directly. No file loading occurs. - Explicit
instruction_file— If set, the file is loaded from the configuredprompts_dir. The path must resolve to a file inside the prompts root (no directory traversal). - Auto-detected file — If neither is set, the agent’s name is converted to
UPPER_SNAKE_CASE.mdand loaded fromprompts_dir.
Basic usage
Name-to-filename conversion
The auto-detection algorithm converts the agent name to a filename using these rules:| Agent Name | Derived Filename | Rule Applied |
|---|---|---|
ChatAgent | CHAT_AGENT.md | CamelCase split on boundaries |
chatagent | CHAT_AGENT.md | Lowercase agent suffix detected and split |
research-assistant | RESEARCH_ASSISTANT.md | Hyphens replaced with underscores |
QA Bot v2 | QA_BOT_V2.md | Spaces and non-alphanumeric chars become underscores |
derive_auto_prompt_filename() internally. It splits camelCase boundaries, normalizes non-alphanumeric characters to underscores, collapses consecutive underscores, and uppercases the result.
Prompts directory resolution
The prompts directory is resolved through its own priority chain:- Explicit
prompts_dirargument on theAgentconstructor. AFK_AGENT_PROMPTS_DIRenvironment variable.- Default:
.agents/promptrelative to the current working directory.
Jinja2 templating
Prompt files support Jinja2 template syntax. When the runner resolves a prompt, it renders the template with a context dictionary that includes agent metadata and any custom context passed to the run. File:.agents/prompt/SUPPORT_AGENT.md
Template context variables
The following variables are available in every prompt template:| Variable | Type | Description |
|---|---|---|
agent_name | str | The agent’s name field. |
agent_class | str | The Python class name of the agent. |
context | dict | The full context dictionary passed to the run. |
ctx | dict | Alias for context (shorthand). |
context dictionary that are not reserved names (context, ctx, agent_name, agent_class) are also available as top-level template variables. So {{ company_name }} works as a shorthand for {{ ctx.company_name }}.
Caching and hot-reload
The prompt system uses a process-widePromptStore singleton that caches at three levels:
-
File cache — Keyed by resolved file path. Uses
stat()metadata (mtime, size, inode) as the cache signature. If the file changes on disk, the cache entry is invalidated automatically. - Text pool — Deduplicates prompt text by SHA-256 hash. If multiple agents use the same prompt content (even from different files), only one copy is stored in memory.
- Template cache — Compiled Jinja2 templates are cached by content hash. Re-rendering with different context variables reuses the compiled template.
os.stat() call per prompt resolution, which is negligible overhead.
Security: path containment
The prompt loader enforces strict path containment. The resolved prompt file path must be inside the configuredprompts_dir. If an instruction_file path resolves outside the prompts root (via ../ traversal or an absolute path pointing elsewhere), the loader raises PromptAccessError immediately.
What to read next
- System Prompts — Full system prompt architecture, resolution pipeline, and design guidelines.
- Agents — Agent model, configuration fields, and composition patterns.
- Security Model — Threat model and defense layers including prompt injection considerations.