AINL Auto-Tuner — Complete Documentation
The autotuneainlcaps program automatically adjusts AINL-related environment variables in OpenClaw to maintain token savings in the 90–95% range. It is production-grade, safe, and designed for any OpenClaw+AINL deployment
AINL Auto-Tuner — Complete Documentation
Overview
The auto_tune_ainl_caps program automatically adjusts AINL-related environment variables in OpenClaw to maintain token savings in the 90–95% range. It is production-grade, safe, and designed for any OpenClaw+AINL deployment.
How It Works
-
Data Collection
ReadsMONITOR_CACHE_JSON(workflow → token_budget) to compute actual daily savings vs. estimated baseline. -
Bridge Analysis
Queries the AINL SQLite bridge DB (ainl_memory.sqlite3) for recent report sizes to determine ifAINL_BRIDGE_REPORT_MAX_CHARSis too loose or too tight. -
Promoter Analysis
Infers promoter cap adequacy from savings trends:- Savings below target → tighten
PROMOTER_LLM_MAX_PROMPT_CHARSandPROMOTER_LLM_MAX_COMPLETION_TOKENS - Savings above target → relax slightly (avoid over-truncation)
- Savings below target → tighten
-
Stability Guard
Requires at least 7 days of history and 5 consecutive days within target range before making adjustments. Changes are limited to ~10–15% per week. -
Output
Writes recommendations to$WORKSPACE/.ainl/tuning_recommendations.jsonand, ifOPENCLAW_AINL_AUTO_APPLY=true, applies them via OpenClawconfig.patch.
Files
intelligence/auto_tune_ainl_caps.lang— main AINL programscripts/run_auto_tune_ainl_caps.sh— runner script (callsrun_intelligence.py)- Optional cron job template (see below)
Setup
1. Ensure AINL integration is complete
Follow docs/AINL_INTEGRATION_GOLDEN.md first. The auto-tuner depends on:
- Pinned workspace env vars
session_context.mdbootstrap preference- Weekly bridge job (produces SQLite history)
MONITOR_CACHE_JSONbeing populated
2. Add cron job (weekly)
Run after the weekly token trends job (e.g., Sunday 11 AM):
openclaw cron add '{
"name": "AINL Auto-Tune Caps",
"schedule": { "kind": "cron", "expr": "0 11 * * 0" },
"payload": {
"kind": "agentTurn",
"agentId": "ainl-advocate",
"message": "run intelligence: auto_tune_ainl_caps"
},
"delivery": { "mode": "announce" },
"sessionTarget": "isolated",
"enabled": true
}'
3. Choose mode
Dry-run (default): The tuner only writes recommendations; you review and apply manually.
Auto-apply: Set environment variable in OpenClaw config:
openclaw gateway config.patch '{
"env": { "vars": { "OPENCLAW_AINL_AUTO_APPLY": "true" } }
}'
Use auto-apply only after verifying dry-run output looks sane for a few weeks.
Configuration Tuning
The program has built-in defaults (see config block in the .lang file). You can override them by setting environment variables before running:
AINL_TUNER_MIN_HISTORY_DAYS(default 7)AINL_TUNER_TARGET_SAVINGS_MIN(default 90.0)AINL_TUNER_TARGET_SAVINGS_MAX(default 95.0)AINL_TUNER_STABLE_DAYS_REQUIRED(default 5)AINL_TUNER_BRIDGE_STEP_FACTOR(default 0.85)AINL_TUNER_PROMOTER_STEP_FACTOR(default 0.90)AINL_TUNER_AUTO_APPLY(default false)
Example: OPENCLAW_AINL_AUTO_APPLY=true AINL_TUNER_TARGET_SAVINGS_MIN=92.0 ./scripts/run_auto_tune_ainl_caps.sh
Outputs
$WORKSPACE/.ainl/tuning_recommendations.json— latest recommendation (human-readable)$WORKSPACE/.ainl/tuning_log.json— history of all runs and applied changes
Recommendation JSON schema
{
"generated_at": "2026-03-25T19:30:00Z",
"savings_pct": 91.2,
"history": [88.1, 89.4, 90.1, 91.0, 91.2, ...],
"current": {
"bridge_chars": 3132,
"promoter_prompt": 4000,
"promoter_completion": 1000
},
"recommended": {
"bridge_chars": 2500,
"promoter_prompt": 3600,
"promoter_completion": 900
},
"bridge_sizes_sample": [2100, 1950, 2300, ...],
"auto_apply": false,
"applied": false,
"reason": "Stable savings for 5 days; bridge size indicates overprovision"
}
Safety & Limits
- Step size caps: Changes are limited to 10–20% per run to avoid sudden breakage.
- Hard bounds: Bridge chars between 500–5000; promoter prompt 1k–10k; completion 200–2k.
- Stability requirement: Will not tighten if 5 consecutive days within target hasn't been achieved.
- Revertibility: All changes go through OpenClaw config history; you can revert via
config.patchor restore previousopenclaw.json. - No destructive actions: The tuner only edits
env.vars; it does not modify code or cron schedules.
Troubleshooting
- No savings computed: Check that
MONITOR_CACHE_JSONcontainsworkflow.token_budget.baseline_estimated_total_tokensandactual_daily_total. These are written by the AINL bridge and intelligence programs. - Bridge sizes empty: Ensure
weekly-token-trendscron is running and writing to SQLite. The tablebridge_reportsmust exist. - Caps not changing: The logic may decide no change is needed; this is normal if you're already within target with stable history.
- Auto-apply not working: Verify
OPENCLAW_BOOTSTRAP_PREFER_SESSION_CONTEXTand other env vars are set; the gateway user must have permission toconfig.patch.
Advanced: Custom Hooks
You can extend the tuner by adding custom checks:
- Promoter truncation detection: parse promoter logs for "truncated" warnings.
- Quality signals: use agent satisfaction ratings if available.
- Multi-agent tuning: per-agent caps could be tuned by analyzing individual agent usage.
Integration Checklist
- [ ] AINL integration golden steps completed
- [ ]
session_context.mdgeneration verified - [ ]
MONITOR_CACHE_JSONhas at least 7 days of data - [ ] Bridge SQLite contains report payloads (
bridge_reportstable) - [ ] OpenClaw
env.varsinclude all pinned paths andOPENCLAW_BOOTSTRAP_PREFER_SESSION_CONTEXT=true - [ ] Dry-run of tuner completes without errors:
scripts/run_auto_tune_ainl_caps.sh - [ ] Review first
tuning_recommendations.json - [ ] (Optional) Enable auto-apply after confirming safe
Maintenance
- Run
openclaw doctor --non-interactiveperiodically to verify environment. - Monitor
tuning_log.jsonfor applied changes and savings trends. - If you manually adjust caps outside the tuner, consider resetting the tuner’s history or allowing it to re-stabilize.
License
Same as AINL core (MIT). Feel free to copy into your own OpenClaw+AINL deployments.
