agent_context: Load context files for an AI coding agent

View source: R/agent_context.R

agent_contextR Documentation

Load context files for an AI coding agent

Description

Returns assembled context (memory, project/global instructions, agent identity files) tailored to a specific consumer. Files autoloaded by the agent natively are skipped to avoid duplication.

Usage

agent_context(
  agent = NULL,
  project_dir = getwd(),
  workspace_dir = NULL,
  memory_base = file.path(path.expand("~"), ".claude", "projects"),
  claude_global_path = file.path(path.expand("~"), ".claude", "CLAUDE.md"),
  include_memory = NULL,
  include_project = NULL,
  include_global = NULL,
  include_soul = NULL,
  max_memory_lines = 100L
)

Arguments

agent

Consumer identifier: "claude", "codex", "corteza", or NULL (interactive / unknown). The legacy "llamar" identifier is accepted as an alias for "corteza".

project_dir

Project directory to scan for CLAUDE.md / AGENTS.md.

workspace_dir

Optional directory containing SOUL.md and USER.md (e.g. ~/.corteza/workspace). If NULL, those files are skipped.

memory_base

Base directory for 'Claude Code' project memory files.

claude_global_path

Path to the global 'Claude Code' instructions file. Defaults to ~/.claude/CLAUDE.md.

include_memory

Override default for memory inclusion. Use TRUE/FALSE to force, or NULL for the agent default.

include_project

Override default for project instructions (CLAUDE.md / AGENTS.md).

include_global

Override default for global instructions (~/.claude/CLAUDE.md / USER.md).

include_soul

Override default for SOUL.md inclusion.

max_memory_lines

Maximum lines to include from each memory source.

Details

Defaults per agent:

  • "claude" - skips Claude Code project memory and CLAUDE.md files (autoloaded by 'Claude Code'). Loads Codex memories, AGENTS.md, USER.md, and SOUL.md when present.

  • "codex" - skips AGENTS.md and Codex memories (autoloaded by Codex). Loads Claude Code project memory, CLAUDE.md, USER.md, and SOUL.md when present.

  • "corteza" or NULL - loads everything available.

Project instructions merge CLAUDE.md and AGENTS.md. The file the consumer autoloads is left out, and so is any heading-delimited section whose text the consumer already holds. Two names for one file, or two identical copies, add nothing; a consumer with no autoloaded file receives CLAUDE.md followed by the sections only AGENTS.md has. Sections are compared verbatim, so the same rule in different words is loaded from both files.

Global instructions are resolved by trying both naming conventions and picking the file relevant to the consumer: ~/.claude/CLAUDE.md or <workspace_dir>/USER.md.

Override the defaults with the include_* parameters.

Value

Character string of assembled context, or empty string if no context applies.

Examples


# Codex agent in current project
saber::agent_context(agent = "codex")

# Corteza with workspace files
saber::agent_context(agent = "corteza",
                     workspace_dir = "~/.corteza/workspace")

# Force-include memory regardless of agent default
saber::agent_context(agent = "claude", include_memory = TRUE)


saber documentation built on Nov. 10, 2026, 9:06 a.m.