AxAgent Memory And Skills Rules (@ax-llm/ax)
This skill helps an LLM generate correct AxAgent memory retrieval, context-map, and dynamic skill-loading code using @ax-llm/ax. Use when the user asks about contextMap, AxAgentContextMap, onMemoriesSearch, memoriesCatalog, recall(…), inputs.memories, onLoadedMemories, onUsedMemories, onSkillsSearch, skillsCatalog, AxAgentCatalogSkill, discover({ skills }), onLoadedSkills, onUsedSkills, preloaded skills, preloading memories at forward time, relevanceRanking hints, loaded memory/skill IDs, or carrying memories across forward() calls.
Install
Install only this skill for TypeScript:
npx skills add https://ax-llm.github.io/ax/typescript/ --skill 'ax-agent-memory-skills'Published skill file: ax-agent-memory-skills/SKILL.md.
Source
- Source: src/ax/skills/ax-agent-memory-skills.md
- Version:
22.0.9
Skill Instructions
Use this skill when an agent needs a persistent context map, task-relevant memory retrieval, or skill guides loaded into the executor prompt on demand. For ordinary agent setup use ax-agent. For RLM runtime policy use ax-agent-rlm. For callbacks and telemetry use ax-agent-observability.
Use These Defaults
- Use a static
skillsCatalog/memoriesCatalogwhen the skill guides or memories fit in a plain array — Ax then backsdiscover({ skills })/recall(...)with a built-in deterministic local search and no host search code is needed. - Use
onSkillsSearch/onMemoriesSearchwhen retrieval needs a real backend (vector DB, BM25 service, KV). A host callback always takes precedence over the catalog’s built-in search. - Use
contextMapwhen repeated runs inspect the same long external context and should accumulate a small orientation cache automatically. recall(...)is available to distiller and executor stages whenonMemoriesSearchor a non-emptymemoriesCatalogis set.discover({ skills })is available to the executor whenonSkillsSearchor a non-emptyskillsCatalogis set.- With
skillsCatalog, the executor prompt also gains a static### Available Skillsindex (id + name + description), so skill discovery is targeted instead of blind. - Both
recall(...)anddiscover({ skills })returnvoid. The loaded content appears on the next turn. - Use
onLoadedMemories/onLoadedSkillsto observe what got loaded. - Use
onUsedMemories/onUsedSkillsto track what the actor says it actually relied on. - Child agents do not inherit memory or skills search callbacks; wire them explicitly on every agent that needs the capability.
Context Map
Use contextMap when repeated runs ask different questions over the same long context, document set, or repository. The map is prompt-resident orientation knowledge: structure, concepts, constants, parsing schema, reusable aggregate results, and concrete error patterns. It is not a task-specific answer cache.
Runnable example: src/examples/rlm-context-map-live.ts demonstrates a provider-backed context-map update, onUpdate snapshot persistence, finite evolve, and frozen map reuse.
When contextMap is configured:
- Ax injects the current map into the distiller prompt.
- Ax updates the map once after each successful completed
forward(...). - By default the map evolves forever. For a finite warmup, create the map with
{ infiniteEvolve: false, evolveSteps: N }; afterNsuccessful updates it is still injected but no longer updated. - Failed runs, aborts, and clarification requests do not update the map.
- Use
onUpdateto persistresult.map.snapshot()outside the agent.
import { agent, AxAgentContextMap } from '@ax-llm/ax';
const map = new AxAgentContextMap(savedSnapshot, {
maxChars: 4000,
infiniteEvolve: false,
evolveSteps: 10,
});
const myAgent = agent('context:string, query:string -> answer:string', {
contextFields: ['context'],
contextMap: {
map,
onUpdate: ({ map }) => saveSnapshot(map.snapshot()),
},
});Types:
type AxAgentContextMapConfig = {
map?: AxAgentContextMap | AxAgentContextMapSnapshot | string;
onUpdate?: (result: AxAgentContextMapUpdateResult) => void | Promise<void>;
};
type AxAgentContextMapOptions = {
maxChars?: number;
infiniteEvolve?: boolean;
evolveSteps?: number;
};Memory Search
Use onMemoriesSearch when the agent needs to pull task-relevant context such as user preferences, prior decisions, project facts, or past conversations from an external store (vector DB, BM25, KV). The actor decides what to load, when, and how much.
When onMemoriesSearch is set, the distiller and executor stages gain:
- An
inputs.memoriesfield. In JS this is an array of{ id, content }entries the actor reads directly. In the prompt, the same entries render as markdown blocks withID: \…`lines, matching the Loaded Skills ID style. Eachcontent` is opaque markdown; frontmatter is not parsed. - A
recall(searches: string[]): voidglobal the actorawaits to load more entries. Recalled entries are appended toinputs.memoriesand visible from the next turn onward.recall()returns nothing.
The responder stage does not receive memories.
Enabling
import { agent } from '@ax-llm/ax';
import type { AxAgentMemoriesSearchFn } from '@ax-llm/ax';
const onMemoriesSearch: AxAgentMemoriesSearchFn = async (
searches,
alreadyLoaded
) => {
// `searches` is the full array passed to recall(...). Batch your
// store lookup in one round-trip.
// `alreadyLoaded` is the current inputs.memories snapshot. Filter
// against it to skip duplicates.
const skip = new Set(alreadyLoaded.map((m) => m.id));
const fresh = await myVectorDB.searchBatch(searches, { topK: 3 });
return fresh.filter((m) => !skip.has(m.id));
};
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
onMemoriesSearch,
});Each memory result must be:
type AxAgentMemoryResult = {
id: string;
content: string;
};Static catalog (no callback)
If the memory set fits in a plain array, skip the callback entirely: pass memoriesCatalog and Ax backs recall(...) with a built-in deterministic local search (idf-weighted token overlap over id + content; not regex, not embeddings). The alreadyLoaded contract is preserved — entries already on inputs.memories are excluded before ranking.
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
memoriesCatalog: [
{ id: 'deploy-window', content: 'Prod deploys only on Tuesday afternoons.' },
{ id: 'user-prefs', content: 'User prefers concise answers.' },
],
});Rules:
- If both
memoriesCatalogandonMemoriesSearchare set, the host callback handles allrecall(...)searches; the catalog still powers the advisoryrelevanceRankinghint. - Catalog content is NOT preloaded into the prompt; entries load only when recalled.
- The built-in search is lexical. For semantic retrieval over large stores, supply
onMemoriesSearchinstead.
Preloading memories at forward time
To seed specific memories for one run (no recall round-trip), pass them as the memories input value. They render on inputs.memories from the first turn and merge with anything recalled later (deduped by id).
await myAgent.forward(ai, {
task: 'Plan the deploy',
memories: [{ id: 'deploy-window', content: 'Prod deploys only on Tuesday afternoons.' }],
});Actor usage
// Turn 1: kick off one batched lookup.
await recall(['user preferences', 'project constraints']);
// Turn 2+: matched entries are now visible on inputs.memories.
const prefs = inputs.memories.find((m) => m.id === 'user-prefs-v2');Rules:
- Pass all memory queries in one
await recall([...])call. - Do not loop
recall()calls or wrap them inPromise.all(...). - Read
inputs.memorieson the next turn to see what landed. recall()invokesonMemoriesSearchwith(searches, alreadyLoaded)and returnsvoid.- Results land on
inputs.memoriesfor subsequent turns and render in the prompt as:
### Memory
ID: `mem:user-prefs-v2`
...- Entries are deduped by
id(last-write-wins) and sorted byidfor prefix-cache stability. - Memories loaded by the distiller thread automatically to the executor. No second
recall()is needed for those entries. recall()may be called multiple times across turns; results accumulate for that run.inputs.memorieslifetime is one.forward()call. It resets between calls.
Carrying Memories Across .forward() Calls
To preserve continuity across calls, persist memories in your store and recall them again on the next call. If you want to replay anything loaded on a prior run, observe loads with onLoadedMemories.
const carried = new Map<string, string>();
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
onMemoriesSearch: async (searches) => {
const fresh = await myVectorDB.searchBatch(searches, { topK: 3 });
const carriedAsResults = [...carried.entries()].map(([id, content]) => ({
id,
content,
}));
return [...carriedAsResults, ...fresh];
},
onLoadedMemories: (results) => {
for (const r of results) carried.set(r.id, r.content);
},
});Skills Search
Use onSkillsSearch when the agent needs to load skill guides such as usage instructions, runbooks, or domain conventions into the executor’s system prompt on demand. The actor decides which skills to fetch and when, so you do not pre-render every skill into every prompt.
When onSkillsSearch is set, the distiller and executor stages gain:
- A “Loaded Skills” section in the system prompt that renders matched skill bodies with stable
ID:values sorted byid. - A
discover({ skills })path the actorawaits to load more skills. Loaded entries appear in the next turn’s prompt.discover(...)returns nothing.
Skills the distiller loads carry over to the executor automatically. The responder does not see skills.
Enabling
import { agent } from '@ax-llm/ax';
import type { AxAgentSkillsSearchFn } from '@ax-llm/ax';
// Each result is { id?: string; name: string; content: string }.
// If id is omitted, Ax falls back to name.
const onSkillsSearch: AxAgentSkillsSearchFn = async (searches) => {
return mySkillStore.resolveBatch(searches, {
// Recommended backend order: exact id, exact name, then broader search.
// This lets the actor pass one simple string and keeps lookup policy host-side.
strategy: ['id', 'name', 'search'],
topK: 2,
});
};
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
onSkillsSearch,
});Each skill result is:
type AxAgentSkillResult = {
id?: string;
name: string;
content: string;
};Static catalog (no callback)
If the skill set fits in a plain array, skip the callback entirely: pass skillsCatalog and Ax backs discover({ skills }) with a built-in deterministic local search (idf-weighted token overlap over id + name×2 + description×2 + the first 600 chars of content; not regex, not embeddings). The executor prompt also gains a static, cache-stable ### Available Skills index (id + name + description, sorted by id), so the actor searches by known ids instead of guessing.
import type { AxAgentCatalogSkill } from '@ax-llm/ax';
const catalog: AxAgentCatalogSkill[] = [
{
id: 'release-checklist',
name: 'Release checklist',
description: 'Steps for shipping a package release safely', // high-signal for matching
content: '1. Bump version\n2. Run tests\n3. Tag and publish',
},
];
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
skillsCatalog: catalog,
});type AxAgentCatalogSkill = {
id: string;
name: string;
description?: string;
content: string;
};Rules:
- If both
skillsCatalogandonSkillsSearchare set, the host callback handles alldiscover({ skills })searches; the catalog still powers the### Available Skillsindex and the advisoryrelevanceRankinghint. - Catalog content is NOT preloaded into the prompt (unlike
skills); entries load only when matched. Useskillsfor guides that must always be in context,skillsCatalogfor a larger set loaded on demand. - The built-in search is lexical. For semantic retrieval over large stores, supply
onSkillsSearchinstead.
Actor usage
// Pass all skill queries in one call.
await discover({ skills: ['release-checklist', 'incident-response'] });
// Next turn: loaded skill bodies render under the "Loaded Skills"
// system-prompt section.
Rules:
discover({ skills })invokesonSkillsSearchwith the raw search strings and returnsvoid.- Resolve each raw string backend-side: prefer an exact
idmatch, then an exactnamematch, then fuzzy/full-text search. The actor should not have to chooseid:vsname:syntax. - Matched skills land under “Loaded Skills” for the next turn.
- Entries are deduped by
id(last-write-wins) and sorted byidfor prefix-cache stability. - If a skill result omits
id, its trimmednameis used as the id for backwards compatibility. - Skills persist on the agent’s
currentSkillsPromptStateacross.forward()calls, unlike memories. - Use
agent.getState()/setState(...)to serialize/restore loaded skills. discover({ skills })may be called multiple times across turns. Within one turn, batch all skill queries in a single call.- Child agents do not inherit
onSkillsSearch; wire it explicitly per agent.
Preloading Skills
If the caller already knows which skills are relevant, pass them up front instead of round-tripping through discover({ skills }).
- Init-time:
skillsonAxAgentOptionsseeds the executor prompt at agent creation. They survivesetState(...)resets. - Forward-time:
skillsonforward(ai, values, { skills })merge in at the start of that call. Distiller and responder ignore forward-time skills.
Both accept the same shape onSkillsSearch returns: readonly AxAgentSkillResult[]. Forward-time skills override init-time skills by id. onLoadedSkills is not fired for preset skills; that callback is for runtime discover({ skills }) analytics.
const releaseAgent = agent('task:string -> answer:string', {
contextFields: [],
skills: [
{
id: 'release-checklist',
name: 'release-checklist',
content: '...',
},
],
});
await releaseAgent.forward(
ai,
{ task: 'Prepare release notes' },
{
skills: [
{
id: 'incident-response',
name: 'incident-response',
content: '...',
},
],
}
);You can use skills without setting onSkillsSearch at all. That is useful for static guides where the actor never needs to fetch more.
Advisory Relevance Hints (relevanceRanking)
relevanceRanking is ON by default in TypeScript — leave it unset; set relevanceRanking: false to opt out. The default was flipped after its A/B gate passed (substance-judged, 49 runs per variant per model: small-model first-lookup precision 24%→90% and answer accuracy 14%→29%; frontier-model control accuracy 63%→88% with fewer turns). The five non-TS ports do not ship the ranker yet, so cross-language behavior diverges on this point until they catch up.
When enabled, a deterministic local ranker scores the agent’s discoverable capabilities against the task once per forward(...) and injects a short advisory ### Likely Relevant shortlist into the executor turn — modules (needs functionDiscovery), catalog skills (needs skillsCatalog), and catalog memories (needs memoriesCatalog). The hint is non-authoritative: the full lists still apply and the actor may discover/recall anything else.
const myAgent = agent('task:string -> answer:string', {
contextFields: [],
functionDiscovery: true,
skillsCatalog: catalog,
relevanceRanking: true, // or { topK: 3, minScore: 0.08 }
});Rules:
- Default is ON (TypeScript); domains still self-gate on their prerequisites (
functionDiscoveryfor modules, catalogs for skills/memories), so agents without those see no change. Everything else in this skill (catalog search, the Available Skills index) is independent of the flag. - The shortlist rides a dynamic, non-cached prompt field; the cached system prompt stays byte-stable across tasks.
- On low confidence the ranker emits nothing rather than guessing.
- Memory hint entries include an ~80-char content snippet; very short memories may be usable from the hint alone without a
recall(...)(such use is not visible toonUsedMemories). - Observe outcomes via the
relevance_rankingcontext event (seeax-agent-observability).
Loaded And Used Tracking
onLoadedMemories reports what recall(...) loaded. onLoadedSkills reports what discover({ skills }) loaded. To track what the actor says it actually relied on, use onUsedMemories / onUsedSkills.
const used: AxAgentUsedMemory[] = [];
await myAgent.forward(
ai,
{ task: 'Make a personal plan' },
{
onUsedMemories: (items) => used.push(...items),
}
);
used; // [{ id, reason, stage }]
Rules:
- The actor can only report memory IDs already present in
inputs.memories. - The actor can only report skill IDs already present in Loaded Skills.
- Unknown values are dropped.
- When tracking is enabled, the actor sees
await used(id, reason?); this is the actor-side declaration mechanism. used(...)resolves against loaded memory IDs and loaded skill IDs.- If memory IDs and skill IDs can collide, namespace them in your application, for example
mem:abcandskill:planning.
Types:
onMemoriesSearch?: AxAgentMemoriesSearchFn;
onLoadedMemories?: (
results: readonly AxAgentMemoryResult[]
) => void | Promise<void>;
onUsedMemories?: (
usedMemories: readonly AxAgentUsedMemory[]
) => void | Promise<void>;
onSkillsSearch?: AxAgentSkillsSearchFn;
onLoadedSkills?: (
results: readonly AxAgentSkillResult[]
) => void | Promise<void>;
onUsedSkills?: (
usedSkills: readonly AxAgentUsedSkill[]
) => void | Promise<void>;
contextMap?: AxAgentContextMapConfig;
skills?: readonly AxAgentSkillResult[];
skillsCatalog?: readonly AxAgentCatalogSkill[];
memoriesCatalog?: readonly AxAgentMemoryResult[];
relevanceRanking?: boolean | { topK?: number; minScore?: number };Examples
Fetch this for full working code:
- RLM Memories and Skills -
onMemoriesSearch+recall()andonSkillsSearch+discover({ skills })with load observability and actual usage tracking viaonUsedMemories/onUsedSkills - Skills + Memory Ops Assistant - an on-call assistant that recalls past decisions from a memory store and loads the right runbook skill on demand (also ported to Python, Go, Rust, Java, and C++ under
src/examples/<lang>/long-agents/). All six languages support the nativeonMemoriesSearch/onSkillsSearchhost callbacks, passed in the agent options at construction (Go/Java use native function values, Rust aagent_with_search_callbacksconstructor, C++ aregister_*_searchhelper); a staticmemory_search_results/skill_search_resultsconfig is also available.
Do Not Generate
- Do not assign the result of
await recall(...)orawait discover(...); both returnvoid. - Do not call
recall()from the responder stage. - Do not call
discover({ skills })from the responder stage. - Do not loop
recall()calls or wrap them inPromise.all(...). - Do not loop
discover()calls or wrap them inPromise.all(...). - Do not assume child agents inherit
onMemoriesSearchoronSkillsSearch. - Do not pass
onMemoriesSearchresults via shared fields as a workaround; userecall(...). - Do not assume
inputs.memoriespersists across.forward()calls. - Do not use
onLoadedMemories/onLoadedSkillsas proof that the actor relied on an item; useonUsedMemories/onUsedSkillsfor actual-use tracking. - Do not write an
onSkillsSearch/onMemoriesSearchcallback that just scans a static array; pass the array asskillsCatalog/memoriesCataloginstead. - Do not rely on the built-in catalog search for semantic matching over large stores; it is lexical token overlap — supply a host callback for embeddings/vector search.
- Do not confuse
skills(always preloaded into the prompt) withskillsCatalog(searchable, loaded on demand).