Dynamic Skill Retrieval During Agent Task Execution
This article explains why agent skill systems often fail not because they lack skills but because retrieval happens too early. It contrasts one-time retrieval at task start with in-process retrieval anchored to the current step, shows how to write queries that express the current gap, and describes a closed loop of retrieval, minimal execution, and explicit validation. It also covers progressive loading, training and evaluation data, and the boundaries and risks of dynamic retrieval. Readers will learn how to make a skill library take effect on demand during complex tasks.
The Problem Isn’t Too Few Skills, It’s Retrieving Too Early
Agent skills typically bundle operating instructions, examples, checklists, and reusable scripts to constrain the steps, tool calls, and acceptance criteria for a class of tasks. For a skill to actually work, two things must hold: you find the right one, and you use it correctly.
A common implementation retrieves once at the start of a task, based on the user’s original request, pulls in a few skills, and then lets the agent run to completion. For tasks with a clear goal and stable input shape, this is often enough. Converting a well-structured plain-text file into a fixed template, for example, lets you determine the relevant skill at the outset.
Complex tasks are different. What format the file actually is, whether a table has merged cells, what error an API returned, whether the previous step’s output is acceptable — all of these change what knowledge the next step truly needs. The user says “organize the materials and generate a report,” and only during execution do you discover that the images are vector format and the fonts shift after export, that the data table can’t be parsed as ordinary cells, and that the final draft still needs a pagination check. That initial goal statement never spelled out these concrete difficulties.
So a mismatch appears: retrieval happens at the moment with the least information, while the moment that most needs new capability shows up mid-execution. If search runs only once at the start, any new problem encountered later can only be handled by the model grinding through trial and error, or by waiting for the user to prompt again. Pre-installing more skills doesn’t automatically solve this, because no matter how large the candidate pool, choosing the wrong moment and the wrong query still stuffs irrelevant instructions into the context.
One-Time Retrieval vs. In-Process Retrieval
It helps to separate the two paths.
A one-time retrieval query is almost identical to the user’s goal. It suits cases where:
- Input and output formats are known in advance;
- There are few failure modes, and they don’t depend on intermediate observations;
- Skill boundaries are clear, and a single skill covers the whole process.
In-process retrieval anchors the query to the current step. What it answers is not “which class of skill does the whole task resemble,” but “which specific capability is missing right now.” Two retrievals serving the same final goal can have very different queries: one is “write a report to spec,” the other is “correctly read a table with merged cells while preserving row and column correspondence.”
Skill retrieval should therefore be treated as part of the execution loop, not a static configuration set before launch. Each time the agent enters a new stage, or fails in the current one, it should decide whether to search again based on the latest observation.
Queries Should Express the Current Gap
A usable current query should contain at least four kinds of information, rather than restating the entire task.
- Stage: reading, converting, formatting, validating, or fixing a failure.
- Observation: the file types, error messages, structural anomalies, or previous outputs already seen.
- Constraint: formats that must be preserved, fields that must not be broken, acceptance criteria.
- Gap: the specific action the model says it can’t do, or that just failed.
A poor query is “help me make a report.” A better one is “parse a table with merged cells, output structured records that don’t misalign rows, and mark the header and data regions.” The former recalls writing skills; only the latter is likely to recall parsing skills.
In practice, short structured notes can replace long restatements, avoiding the use of an entire conversation history as retrieval text. Early goals in the history dilute the current signal, pushing the retriever toward “broad and comprehensive” skills instead of ones that resolve the immediate obstacle.
A schematic, product-neutral query construction might look like this:
stage: parse_tableobserved: merged_cells=true, header_rows=2constraint: keep_row_alignmentgap: cannot map merged header to columnsask: procedure + validation checks
This text describes only the current need and isn’t tied to any vendor interface. Once retrieval returns, first check the skill’s applicable scope and prerequisites, then decide whether to load the body, examples, or scripts.
Progressive Loading Still Needs the Right Timing
Progressive loading usually means giving the skill name and a short description first, then loading detailed steps and resources once relevance is confirmed. It can save context, but it can’t replace in-process retrieval.
If the candidate pool itself was scoped at task start by a vague goal, then even expanding step by step only picks details from the wrong set. Problems like merged cells, font fallback, and pagination checks may not be in the initial candidates at all. No amount of detailed loading can add back a capability that was never recalled.
A more reasonable order is:
- First retrieve by the current gap to get a few candidates;
- Expand only the single best-matching description;
- Execute one small step per the instructions and run a check;
- If the check fails, write the failure into the next query rather than blindly retrying within the same long description.
This keeps the number of skills present in context small and strongly relevant to the current step.
Retrieval, Execution, and Validation Form a Closed Loop
Skill retrieval shouldn’t end after returning a description. It’s better to make single-step execution a fixed closed loop.
Match check. Compare the skill’s declared input conditions against the current observation. If conditions don’t match, don’t load the full text — move to the next candidate or rewrite the query.
Minimal execution. Do only the one step the skill covers, for example parsing the table into aligned records first, rather than rewriting the whole document at once. The smaller the scope, the easier it is to attribute a failure.
Explicit validation. The skill should state checkable results: whether row and column counts match, whether merged regions were expanded or annotated, whether the exported text still contains key fields, whether pagination truncates headings. Without acceptance criteria, it’s easy for the model to “look like it finished.”
Write back failures. Condense the error into the observation and gap of the next query, rather than piling the entire raw error into the prompt. If consecutive retrievals don’t match, confirm the missing input or constraint with the user instead of looping indefinitely.
A report task can naturally be split into several retrieval points: identifying material types, parsing tables and images, organizing facts and citation relationships, generating the body from a template, and finally checking layout and consistency. The skills for each segment need not be the same, nor all loaded in the first segment.
Making Retrieval Match the Agent’s Real Needs
Using only the user’s original words for vector retrieval misaligns the optimization target with the decisions made during execution. More useful training or evaluation samples should be “current state to required skill” pairs, not “initial goal to some overall skill” pairs.
When constructing such samples, you can record:
- The facts already known before reaching this step;
- The action that just failed or is about to begin;
- The skill that actually proved useful at the time;
- Typical mis-recalled skills when the wrong one was used, as negative examples.
Evaluation shouldn’t only check whether the retrieval list contains a given skill. More meaningful questions are: on the same task, did in-process retrieval reduce ineffective retries, did it recall the corresponding skill when a key obstacle appeared, and did the final output pass the pre-written checks. These judgments can be made on a self-built task set, without relying on unverified external benchmark numbers.
The retriever can also use skill metadata: applicable file types, required tool categories, unsupported cases, output shapes. Metadata is for filtering; the body is for confirmation. Filtering blocks obviously irrelevant skills, and confirmation avoids loading based on the title alone.
Boundaries and Risks in Practice
Dynamic retrieval adds calls and latency, so set trigger conditions rather than searching after every generated sentence. Signals that suit triggering include: entering a new stage, a tool returning an error, a failed validation, or the current skill declaring it doesn’t cover the input. For short, well-defined tasks, still keep one-time retrieval to avoid needless overhead.
Also watch permissions and context hygiene. Scripts and examples in a skill package should load only when the task truly needs them; retrieval logs shouldn’t contain keys, personal data, or unauthorized internal material. Skill descriptions should come from reviewable sources and pass basic security checks, to prevent handing uncontrolled operating steps straight to an executor.
Another common mistake is treating retrieval as a substitute for planning. Retrieval can only supplement “how to do this step properly”; it can’t invent business rules out of nothing. The goal, data scope, and acceptance criteria must still be given by the user or an upstream process. Skills solve capability gaps, not ambiguity in the requirement itself.
Practical Takeaways
- Have the opening retrieval handle only already-defined subgoals, and state its coverage.
- Write queries with the current gap, observation, and constraints, not a restatement of the whole user request.
- Load skills in tiers: metadata, steps, examples, scripts, expanded progressively.
- Attach an executable check to each step, and re-retrieve with new observations after a failure.
- Limit the number of skills loaded at once to keep irrelevant instructions from crowding the context.
- Use a self-built task log of “state — skill — passed validation” to continuously correct retrieval, rather than only adding skill entries.
Back to the opening report scenario: font shifts, merged cells, and pagination are not something a single “can write reports” skill can anticipate in advance. Moving search from the task’s starting point to the moment the need appears, making the query describe the concrete obstacle in front of you, and using validation results to decide whether to keep retrieving — only then does the skill library take effect on demand in complex tasks, instead of looking well-prepared at the start while the agent keeps grinding through execution.
