Skip to content

Commit e768059

Browse files
committed
docs(agents): refine context efficiency guidance
1 parent 6615caa commit e768059

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

config/agents/rules/08-context-efficiency.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ waiver_path: .agents/waivers/AGENT-08.md
1010

1111
Prefer precise, filtered queries over dumping large datasets. A few tokens filtering at the source saves many tokens of noise downstream.
1212

13-
- Prefer `jg` over `jq` for structured data queries; use `jq` only when you need transformation or `jg` cannot express the query.
13+
- Prefer `jg` over `jq` for structured data queries when `jg` is available; if `command -v jg` fails, use tool-native selectors, `python3 -c`, or `jq` instead of retrying.
1414
- Use structured output (`--json`, `-o json`) when available
15-
- Filter at source with `jg`, `grep`, SQL `WHERE`, or API query params
15+
- Filter at source with `jg` when available, grep, SQL `WHERE`, or API query params
1616
- Never dump an entire response to find one field
17+
- Bound commands before they run: add path scopes, `--limit`, `head`, field selectors, or SQL `LIMIT`; do not raise output caps to compensate for broad dumps.
18+
- If output truncates, rerun a narrower command instead of scanning the dump.
1719

1820
```
1921
tool --output json | jg 'precise selector' # good

flake.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

skills/catalog/context-efficiency/SKILL.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
name: context-efficiency
33
description: >
44
Filter data at the source before it enters context. Use when querying APIs,
5-
CLIs, or databases where full output would be large. Prefer jg filtering over
6-
dumping and scanning; use jq/python only when jg cannot express the needed
7-
transformation. Trigger phrases:
5+
CLIs, or databases where the full output would be large. Prefer structured
6+
output + jg when available, or python/tool-native selectors when not. Use jq
7+
only when jg cannot express the needed transformation. Trigger phrases:
88
"find the entity for", "get the ID of", "which service handles", "what's the
99
value of", or any time you'd otherwise dump a large dataset to find one thing.
1010
license: MIT
@@ -19,34 +19,32 @@ of reasoning. Spend a few tokens on a precise query; save many on the backend.
1919

2020
```
2121
Does the tool support structured output? (--json, -o json, --format json)
22-
├─ Yes → use it, then filter with jg; use jq/python only for transformations jg cannot express
23-
└─ No → use grep to pre-filter, or hit the raw API with query params
22+
├─ Yes → select fields with jg when available; if `command -v jg` fails, use tool-native selectors, python3 -c, or jq instead of retrying
23+
└─ No → pre-filter with path scopes, grep, head, API params, or SQL LIMIT
2424
```
2525

26-
## jq/python fallback patterns
27-
28-
Use these only when `jg` cannot express the needed transformation.
26+
## Structured-output patterns
2927

3028
```bash
31-
# Get one field from an array of objects
32-
tool -o json | jq -r '.[0].field'
3329

34-
# Filter array by condition
35-
tool -o json | jq '[.[] | select(.state == "on")]'
30+
# Select one field or path
31+
tool -o json | jg 'precise.selector'
3632

37-
# Case-insensitive name search
38-
tool -o json | jq -r '.[] | select(.attributes.friendly_name | test("couch"; "i")) | .entity_id'
3933

40-
# Count by group
41-
tool -o json | jq 'group_by(.state) | map({state: .[0].state, count: length})'
34+
# Filter to the few fields needed
35+
tool -o json | jg 'items matching condition -> id,name,status'
4236

43-
# Pluck two fields
44-
tool -o json | jq -r '.[] | [.entity_id, .state] | @tsv'
37+
38+
# Transform when selection is not enough
39+
tool -o json | python3 -c 'import json,sys; print(json.load(sys.stdin)[0]["field"])'
4540
```
4641

47-
## python3 -c patterns
42+
## jq/python fallback patterns
43+
44+
Use these only when `jg` cannot express the needed transformation.
4845

4946
```bash
47+
# python3 -c patterns
5048
# Count by state
5149
tool -o json | python3 -c "
5250
import json, sys; d = json.load(sys.stdin)
@@ -60,6 +58,14 @@ print(next(x['entity_id'] for x in d if 'couch' in x['attributes'].get('friendly
6058
"
6159
```
6260

61+
## Bounded command output
62+
63+
Before running commands that can dump a tree, log, search result, build output,
64+
or API collection, add a path scope, `--limit`, `head`, structured selector, or
65+
SQL `LIMIT`. Do not raise output-token caps to compensate for an unbounded
66+
command. If output truncates, rerun a narrower command instead of scanning the
67+
dump.
68+
6369
## Oversized tool results
6470

6571
If a read, MCP call, or CLI returns "output too large" or saves overflow to a
@@ -86,23 +92,23 @@ tool search --query '"exact error" after:2026-01-01' --limit 5
8692
### Home Assistant (hass-cli)
8793

8894
```bash
89-
hass-cli -o json state list 'light.*' | jq -r '.[] | select(.state=="on") | .entity_id'
90-
hass-cli -o json area list | jq -r '.[] | [.area_id, .name] | @tsv'
91-
hass-cli -o json device list | jq '[.[] | select(.area_id == "kitchen")]'
95+
hass-cli -o json state list 'light.*' | jg 'entity_id,state,attributes.friendly_name'
96+
hass-cli -o json area list | jg 'area_id,name'
97+
hass-cli -o json device list | jg 'devices in kitchen -> id,name,area_id'
9298
```
9399

94100
### GitHub CLI
95101

96102
```bash
97-
gh pr checks 42 --json name,state | jq -r '.[] | select(.state=="FAILURE") | .name'
98-
gh issue list --json number,title,labels | jq '[.[] | select(.labels[].name == "bug")]'
103+
gh pr checks 42 --json name,state | jg 'failed check names'
104+
gh issue list --json number,title,labels --limit 30 | jg 'issues with bug label'
99105
gh api repos/:owner/:repo/pulls --jq '.[].head.ref'
100106
```
101107

102108
### Nix
103109

104110
```bash
105-
nix eval .#nixosConfigurations.nuc.config.environment.systemPackages --json | jq -r '.[].name' | grep hass
111+
nix eval .#packages --json | jg 'package names'
106112
```
107113

108114
### Database
@@ -119,11 +125,11 @@ SELECT entity_id, state FROM states WHERE domain = 'light' ORDER BY last_changed
119125
hass-cli state list
120126

121127
# ✅ returns exactly what you need
122-
hass-cli -o json state list 'light.*' | jq -r '.[] | select(.attributes.friendly_name | test("desk"; "i")) | .entity_id'
128+
hass-cli -o json state list 'light.*' | jg 'entity_id,state,attributes.friendly_name matching desk'
123129

124130
# ❌ loads full PR list into context
125131
gh pr list
126132

127133
# ✅ targeted
128-
gh pr list --json number,title --jq '.[] | select(.title | test("fix"; "i"))'
134+
gh pr list --json number,title --limit 30 | jg 'titles matching fix'
129135
```

0 commit comments

Comments
 (0)