skill-chain-context-optimization

Refactor large or frequently-run skills into context-efficient chains using isolated execution, file-backed handoffs, minimal summaries, and runtime-aware command substitution.

5 stars

Best use case

skill-chain-context-optimization is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Refactor large or frequently-run skills into context-efficient chains using isolated execution, file-backed handoffs, minimal summaries, and runtime-aware command substitution.

Teams using skill-chain-context-optimization should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/skill-chain-context-optimization/SKILL.md --create-dirs "https://raw.githubusercontent.com/vamseeachanta/workspace-hub/main/.agents/skills/coordination/skill-chain-context-optimization/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/skill-chain-context-optimization/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How skill-chain-context-optimization Compares

Feature / Agentskill-chain-context-optimizationStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Refactor large or frequently-run skills into context-efficient chains using isolated execution, file-backed handoffs, minimal summaries, and runtime-aware command substitution.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Skill Chain Context Optimization

Use this when reviewing or improving a skill that runs multiple steps, loops over many items, calls tools repeatedly, or returns large intermediate data. The goal is to keep the main agent context limited to decisions, summaries, and final artifacts rather than raw tool outputs.

## Source Pattern

This skill captures a 2026 skill-chaining pattern from the reviewed YouTube video `JdqJ2ekWt8M`:

1. Monolithic skill chaining bloats the working window because each step leaves bulky scrape/search/tool material in the active run.
2. The scalable pattern is: isolate execution, write minimal per-step artifacts to files, and feed only the next step's required fields forward.
3. The cited benchmark claimed roughly **51K tokens** added for the monolithic version versus **5-8K tokens** for the forked/file-backed version, about **85% lower context burn**.

Treat those numbers as directional evidence; verify against our own session/provider logs before making broad claims.

## When to Apply

Apply this pattern to skills that meet at least one of these conditions:

- `SKILL.md` is large (rough guide: >400 lines or >20 KB).
- The workflow loops over multiple records, issues, repos, documents, leads, files, or providers.
- The workflow has 3+ logically distinct stages where later stages only need a subset of earlier output.
- Tool outputs are large, repetitive, or mostly useful only for extraction.
- The skill is run frequently enough that token/context cost matters.

Do **not** apply this pattern mechanically to small one-shot skills. Extra orchestration can make simple skills harder to use.

## Three-Layer Refactor Pattern

### 1. Isolate heavy work from the orchestrator

Keep the orchestrator responsible for:

- loading the skill,
- deciding the stage order,
- checking gates,
- receiving compact status lines,
- producing the final user-facing result.

Move heavy tool use into isolated execution:

- Hermes: prefer `delegate_task` for reasoning-heavy isolated work, or `execute_code`/scripts for deterministic loops.
- Codex: use runtime-supported skill/agent forking if available in that environment.
- Codex/Gemini: use separate CLI invocations or file-based prompts when isolation is needed.

Subworkers should return compact status, not raw logs. For detailed evidence, they should write an artifact path and report that path.

### 2. Use file-backed handoffs between stages

Create a run-scoped artifact directory, for example:

```text
.Codex/tmp/skill-runs/<skill-name>/<YYYYMMDD-HHMMSS>-<short-id>/
  00-input.json
  10-profile.json
  20-company.md
  30-signals.json
  40-score.json
  90-final-summary.md
  errors.jsonl
```

Rules:

- Each stage writes only the fields needed by downstream stages.
- Prefer JSON for structured state and Markdown for human review artifacts.
- Include provenance fields: source path/URL, timestamp, command/script used, and confidence where applicable.
- Redact secrets and avoid writing sensitive raw payloads unless the skill explicitly requires it.
- Add cleanup guidance for high-volume temp directories; do not let `.Codex/tmp` grow indefinitely.

### 3. Feed forward only required context

For each stage, define an explicit input contract and output contract:

```markdown
## Stage: Score Candidate

Inputs:
- `10-profile.json`: `name`, `role`, `company`, `high_signal_facts[]`
- `20-company.md`: 5-10 line company brief
- `references/scoring-rubric.md`: static rubric

Output:
- `40-score.json`: `score`, `rationale[]`, `disqualifiers[]`, `next_action`

Return to orchestrator:
- one line: `score=<n>; next_action=<...>; artifact=40-score.json`
```

If the runtime supports parse-time command substitution such as ``!`cat artifact.json` ``, use it only for small, already-distilled artifacts. Otherwise use scripts or tool calls to read the artifact and pass the minimal content explicitly. Never substitute large raw scrape/search outputs.

## Refactor Procedure

1. **Inventory the current skill**
   - Count lines/chars and identify large sections.
   - Mark stage boundaries.
   - Identify repeated tool calls and raw-output handoffs.
   - Identify static references that belong in `references/`, templates, or scripts instead of inline prose.

2. **Split into orchestrator + stages**
   - Keep `SKILL.md` short enough to explain routing and gates.
   - Move reusable static examples into `references/` or `templates/`.
   - Move deterministic transformations into `scripts/`.
   - For complex reasoning stages, create sub-skills or explicit worker prompts.

3. **Define artifact contracts**
   - Write input/output schemas for every stage.
   - Include success and failure shapes.
   - Require workers to write detailed evidence to files and return only compact status.

4. **Add error propagation**
   - Each stage must report `status: ok|blocked|failed`.
   - Failures must include `reason`, `repro_or_source`, `artifact_path`, and `recommended_next_action`.
   - The orchestrator must stop or degrade explicitly rather than guessing.

5. **Measure before/after**
   - Capture approximate context/token burn before refactor where available.
   - Compare main-conversation output volume, artifact size, runtime, and user-visible quality.
   - Keep measured claims in reports; avoid unverified token savings claims in skill front matter.

## Runtime Compatibility Notes

- The YouTube pattern references Codex features such as skill forking and command substitution. Do not assume identical syntax works in Hermes, Codex, or Gemini.
- In Hermes, `delegate_task` already provides isolated child contexts, but only the final summary returns. If detailed work matters, instruct the subagent to write a named artifact in the repo/workdir and return the path.
- For deterministic extraction, prefer `execute_code` or checked-in scripts over LLM reasoning.
- For unattended cron jobs, prompts must be self-contained and cannot ask clarifying questions; use file contracts and explicit failure artifacts.

## Candidate Audit Heuristic

A quick filesystem-only first pass:

```bash
python - <<'PY'
from pathlib import Path
root = Path('.Codex/skills')
rows = []
for p in root.rglob('SKILL.md'):
    if '_archive' in p.parts:
        continue
    text = p.read_text(errors='ignore')
    lines = text.count('\n') + 1
    chars = len(text)
    signals = sum(s in text.lower() for s in ['loop', 'batch', 'parallel', 'search', 'scrape', 'research', 'review'])
    if lines > 400 or chars > 20000 or signals >= 3:
        rows.append((lines, chars, signals, p))
for lines, chars, signals, p in sorted(rows, reverse=True)[:30]:
    print(f'{lines:4d} {chars:6d} signals={signals} {p}')
PY
```

Use this as a triage list only. A large reference-heavy skill may be fine if it is rarely loaded or already file-backed.

## Validation Checklist

Before landing a context-optimization change:

- [ ] Orchestrator returns compact summaries and artifact paths only.
- [ ] Each stage has explicit input/output contracts.
- [ ] Raw tool outputs are not copied into downstream prompts unless required.
- [ ] Detailed evidence is written to files under a run-scoped artifact directory.
- [ ] Failure artifacts are machine-readable enough for reruns.
- [ ] Cleanup/retention guidance exists for temp artifacts.
- [ ] Runtime-specific features are labeled by provider/tool; no unsupported syntax is presented as universal.
- [ ] A before/after metric or at least a qualitative context-bloat rationale is recorded.

## Good Targets in workspace-hub

From a quick active skill inventory on 2026-04-26, no active skills used an explicit `context_fork`/`context fork` marker, while many large skills already mention temp/handoff artifacts. Prioritize optimization reviews for large, frequently loaded orchestration skills such as GitHub planning/execution, provider/session audits, research pipelines, and overnight batch workflows.

Related Skills

context-compaction-handoff

5
from vamseeachanta/workspace-hub

Guardrails for resuming work after context compaction or transcript handoff blocks; prioritize the latest real user request over stale summarized tasks and verify before answering.

wiki-context

5
from vamseeachanta/workspace-hub

Auto-query llm-wiki domains for relevant context before executing domain tasks

data-context-extractor

5
from vamseeachanta/workspace-hub

Generate or improve a company-specific data analysis skill by extracting tribal knowledge from analysts

usage-optimization

5
from vamseeachanta/workspace-hub

Optimize AI usage efficiency through script-first patterns, batch operations, and input preparation

core-context-management-windows-task-scheduler

5
from vamseeachanta/workspace-hub

Sub-skill of core-context-management: Windows Task Scheduler (+2).

core-context-management-validatecontextsh

5
from vamseeachanta/workspace-hub

Sub-skill of core-context-management: validate_context.sh (+4).

core-context-management-size-limits-mandatory

5
from vamseeachanta/workspace-hub

Sub-skill of core-context-management: Size Limits (MANDATORY) (+3).

core-context-management-response-format-rules

5
from vamseeachanta/workspace-hub

Sub-skill of core-context-management: Response Format Rules (+3).

core-context-management-learning-from-past-work

5
from vamseeachanta/workspace-hub

Sub-skill of core-context-management: Learning from Past Work (+2).

core-context-management

5
from vamseeachanta/workspace-hub

Manage agent context files, enforce size limits, and optimize token budget allocation

complexity-scorer-3-context-aware-scoring

5
from vamseeachanta/workspace-hub

Sub-skill of complexity-scorer: 3. Context-Aware Scoring (+1).

docker-1-image-optimization

5
from vamseeachanta/workspace-hub

Sub-skill of docker: 1. Image Optimization (+4).