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
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# OpenClaw Integration

Make your [OpenClaw](https://docs.openclaw.ai) agent **self-improving**. ACE reads session transcripts, extracts what worked and what didn't, and feeds learned strategies back into the agent's context via a skillbook β€” automatically, every session.

---

## What Is OpenClaw?

[OpenClaw](https://github.com/openclaw/openclaw) is an open-source, self-hosted AI assistant gateway. It connects AI models (Claude, GPT, etc.) to messaging platforms like Telegram, WhatsApp, Discord, and more. It runs locally and stores all data β€” sessions, memory, configuration β€” as files on your machine under `~/.openclaw/`.

ACE plugs into this by reading session transcripts and building a skillbook of learned strategies that the agent loads at session start.

---

## How It Works

```mermaid

flowchart TD

    A["OpenClaw session ends"] --> B["Transcript saved to<br><code>~/.openclaw/agents/main/sessions/*.jsonl</code>"]

    B --> C["<b>ace-learn</b><br>session start or on-demand"]

    C --> D["LoadTracesStep β†’ OpenClawToTraceStep"]

    D --> E["<b>TraceAnalyser</b><br>Reflect β†’ Update β†’ Apply"]

    E --> F["<code>ace_skillbook.json</code><br>machine-readable"]

    E --> G["<code>ace_skillbook.md</code><br>human-readable"]

    G --> H["AGENTS.md tells agent<br>to read skillbook"]

    H --> I["Agent loads strategies<br>into context"]

```

1. OpenClaw writes session transcripts to `~/.openclaw/agents/<id>/sessions/*.jsonl`
2. `ace-learn` runs at the start of the next session (or on-demand)
3. **LoadTracesStep** reads JSONL files into raw event lists
4. **OpenClawToTraceStep** converts events into structured traces
5. **TraceAnalyser** runs the learning pipeline (Reflect β†’ Update β†’ Apply)
6. Updated skillbook is written to the workspace volume
7. The agent reads `ace_skillbook.md` into its context and applies relevant strategies

---

## Prerequisites

Before setting up ACE, you need a working OpenClaw installation.

!!! info "Platform support"
    OpenClaw runs on **Linux**, **macOS**, and **Windows** (via WSL2). All shell commands on this page use bash syntax. On Windows, run them inside your WSL2 environment. The `setup.py` script uses cross-platform Python and works on all three platforms natively.


### 1. Install OpenClaw

!!! note "Already have OpenClaw running?"
    Skip to [Setup Methods](#setup-methods) below.


=== "npm (quickest)"

    ```bash

    npm install -g openclaw@latest

    openclaw onboard --install-daemon

    ```


    The onboard wizard walks you through model provider setup, API keys, and optional channel connections (Telegram, WhatsApp, etc.).


=== "Docker"

    ```bash

    git clone https://github.com/openclaw/openclaw.git

    cd openclaw

    ./docker-setup.sh

    ```


    The setup script builds the image, runs onboarding, and starts the gateway via Docker Compose.


=== "From source"

    ```bash

    git clone https://github.com/openclaw/openclaw.git

    cd openclaw

    pnpm install && pnpm ui:build && pnpm build

    pnpm openclaw onboard --install-daemon

    ```


For full details, see the [OpenClaw documentation](https://docs.openclaw.ai).

### 2. Verify OpenClaw is working

Make sure the gateway is running and you have at least one completed session:

```bash

# Check the gateway is up

curl -fsS http://127.0.0.1:18789/healthz



# Check sessions exist

ls ~/.openclaw/agents/main/sessions/*.jsonl

```

### 3. Get an LLM API key for ACE

ACE needs its own LLM API key to run the reflection model. This is separate from the key OpenClaw uses. Any [LiteLLM-supported provider](https://docs.litellm.ai/docs/providers) works:

| Provider | Key variable | Example model |
|----------|-------------|---------------|
| Anthropic | `ANTHROPIC_API_KEY` | `anthropic/claude-sonnet-4-6` |
| OpenRouter | `OPENROUTER_API_KEY` | `openrouter/anthropic/claude-sonnet-4-6` |
| AWS Bedrock | `AWS_BEARER_TOKEN_BEDROCK` | `bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0` |
| LiteLLM proxy | `LITELLM_API_KEY` | `anthropic/claude-sonnet-4-5` |

---

## Setup

Two steps: **install the skill** (copies files + patches AGENTS.md), then **choose how to run** the learning script.

### Step 1 β€” Install the skill

The skill needs to be copied into your OpenClaw workspace and AGENTS.md needs to be updated so the agent knows to use the skillbook. You can do this automatically with the setup script or manually.

=== "Automatic (setup script)"

    Clone the ACE repo and run the setup script:


    ```bash

    git clone https://github.com/Kayba-ai/agentic-context-engine.git

    cd agentic-context-engine

    python examples/openclaw/setup.py

    ```


    This does two things:


    1. **Copies** the `kayba-ace/` skill folder to `~/.openclaw/workspace/skills/kayba-ace/`
    2. **Appends** auto-learning instructions to `~/.openclaw/workspace/AGENTS.md`

    Options:


    ```bash

    python examples/openclaw/setup.py --no-agents        # skip AGENTS.md patching

    python examples/openclaw/setup.py --openclaw-home /path/to/.openclaw  # custom path

    ```


    The script is idempotent β€” it won't overwrite generated files (`ace_skillbook.json`, `ace_skillbook.md`, `ace_processed.txt`) and skips the AGENTS.md patch if already present.


=== "Manual"

    **1. Copy the skill folder** into the OpenClaw workspace:


    ```bash

    # Clone the ACE repo (if you haven't already)

    git clone https://github.com/Kayba-ai/agentic-context-engine.git


    # Copy the skill

    mkdir -p ~/.openclaw/workspace/skills/kayba-ace

    cp agentic-context-engine/examples/openclaw/kayba-ace/* \

       ~/.openclaw/workspace/skills/kayba-ace/

    ```


    **2. Patch AGENTS.md** β€” append the auto-learning instructions so the agent reads the skillbook at session start:


    ```bash

    cat agentic-context-engine/examples/openclaw/AGENTS.md.snippet \

      >> ~/.openclaw/workspace/AGENTS.md

    ```


    Or copy the snippet content manually and paste it at the end of your `AGENTS.md`. The snippet tells the agent to:


    - Run `ace-learn` at session start and report results
    - Read `skills/kayba-ace/ace_skillbook.md` into its context
    - Cite strategy IDs when applying learned strategies

    !!! warning "Check for duplicates"

        If you run the manual steps more than once, make sure you don't append the snippet twice. Look for the `## Auto-Learning` heading in your AGENTS.md β€” if it's already there, skip this step.


### Step 2 β€” Choose how to run learning

| | Docker (recommended) | Host |
|---|---|---|
| **How it works** | Bakes ACE into the OpenClaw Docker image | Runs ACE on your host machine |
| **Learning trigger** | Agent runs `ace-learn` at session start | Cron job or manual |
| **Pros** | Zero runtime setup, fully automatic | No Docker customization needed |
| **Cons** | Requires rebuilding the image | Agent can't trigger learning itself |

---

## Docker Setup (Recommended)

Extends your OpenClaw Docker image with Python 3.12 and the ACE framework pre-installed. The agent runs `ace-learn` at session start automatically.

#### 2a β€” Get the Dockerfile

```bash

# From the ACE repo (already cloned in Step 1)

cp examples/openclaw/Dockerfile.ace /path/to/your/openclaw/

```

Or download it directly:

```bash

curl -o Dockerfile.ace \

  https://raw.githubusercontent.com/Kayba-ai/agentic-context-engine/main/examples/openclaw/Dockerfile.ace

```

#### 2b β€” Build the image

From your OpenClaw directory:

```bash

# Build the base OpenClaw image first (if not already built)

docker build -t openclaw:base .



# Extend with ACE

docker build -t openclaw:local --build-arg OPENCLAW_IMAGE=openclaw:base -f Dockerfile.ace .

```

!!! info "What this installs"
    The extended image adds ~200MB and includes:


    - **uv** β€” Python package manager
    - **Python 3.12** β€” via uv standalone builds (the base image ships 3.11)
    - **ACE framework** β€” cloned from GitHub at `/opt/ace` with all dependencies
    - **`ace-learn`** β€” wrapper script at `/usr/local/bin/ace-learn`

Then point your OpenClaw setup at the new image. In your `.env` file:

```bash

OPENCLAW_IMAGE=openclaw:local

```

#### 2c β€” Pass your API key

Add the ACE reflection key to your `docker-compose.yml` environment section (or `.env` file):

```yaml

services:

  openclaw-gateway:

    environment:

      # ... existing keys ...

      # Add ONE of these depending on your provider:

      AWS_BEARER_TOKEN_BEDROCK: ${AWS_BEARER_TOKEN_BEDROCK}

      ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY}

      OPENROUTER_API_KEY: ${OPENROUTER_API_KEY}

      LITELLM_API_KEY: ${LITELLM_API_KEY}

      # Optional: override the default reflection model

      ACE_MODEL: ${ACE_MODEL:-bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0}

```

The default model is `bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0`. Set `ACE_MODEL` in your `.env` to override.

#### 2d β€” Restart and verify

```bash

docker compose down && docker compose up -d openclaw-gateway

```

Send a message to your agent (e.g., via Telegram). It should:

1. Run `ace-learn` and report what it found
2. Read the skillbook into its context
3. Respond to your message, citing strategy IDs when relevant

You can also test directly:

```bash

# Dry run β€” parses sessions without making LLM calls

docker run --rm -v ~/.openclaw:/home/node/.openclaw openclaw:local ace-learn --dry-run



# Full run

docker run --rm \

  -v ~/.openclaw:/home/node/.openclaw \

  -e AWS_BEARER_TOKEN_BEDROCK="$AWS_BEARER_TOKEN_BEDROCK" \

  openclaw:local ace-learn

```

---

## Host Setup

Run ACE on the host machine (outside Docker). This reads session files directly from disk. Useful if you don't want to customize the Docker image.

#### 2a β€” Install ACE dependencies

From the ACE repo (already cloned in Step 1):

```bash

cd agentic-context-engine

uv sync

```

!!! note "Python 3.12+ required"
    Check with `python3 --version`. Install [uv](https://docs.astral.sh/uv/) if you don't have it.


#### 2b β€” Configure your API key

=== "Anthropic"

    ```bash

    export ANTHROPIC_API_KEY="sk-ant-..."

    ```


=== "OpenRouter"

    ```bash

    export OPENROUTER_API_KEY="sk-or-..."

    export ACE_MODEL="openrouter/anthropic/claude-sonnet-4-6"

    ```


=== "AWS Bedrock"

    ```bash

    export ACE_MODEL="bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0"

    ```


    Uses AWS SDK auth β€” no explicit key needed if credentials are configured.


You can also put these in `~/.openclaw/.env` or `~/.env` β€” the script loads both via `python-dotenv`.

#### 2c β€” Verify and run

```bash

cd /path/to/agentic-context-engine



# Dry run (no LLM calls, just parse sessions)

uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py --dry-run



# Learn from all new sessions

uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py



# Process specific files

uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py \

  ~/.openclaw/agents/main/sessions/f967d602.jsonl



# Reprocess everything

uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py --reprocess

```

#### 2d β€” Automate (optional)

=== "Linux / macOS (cron)"

    ```bash

    crontab -e

    ```


    Add:


    ```

    */30 * * * * cd /path/to/agentic-context-engine && uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py >> /tmp/ace-openclaw.log 2>&1

    ```


=== "Windows (Task Scheduler)"

    Create a scheduled task that runs every 30 minutes:


    ```powershell

    # From an elevated PowerShell prompt

    $action = New-ScheduledTaskAction `

      -Execute "wsl" `

      -Argument "bash -c 'cd /path/to/agentic-context-engine && uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py >> /tmp/ace-openclaw.log 2>&1'"

    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 30)

    Register-ScheduledTask -TaskName "ACE Learn" -Action $action -Trigger $trigger

    ```


    This calls into WSL2 where OpenClaw and ACE are installed.


=== "Manual"

    Run the script whenever you want to learn from new sessions:


    ```bash

    cd /path/to/agentic-context-engine

    uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py

    ```


!!! note "AGENTS.md for host setup"
    The setup script already patched AGENTS.md in Step 1. For the host setup, the agent can't run `ace-learn` directly (it's not in the container), so it will report that `ace-learn` is not found and continue normally. Learning happens externally via cron or manual runs; the agent still reads the skillbook at session start.


---

## Output Files

The learning script writes these files to the skill directory:

| File | Format | Description |
|---|---|---|
| `ace_skillbook.json` | JSON | Machine-readable skillbook (persists across runs) |
| `ace_skillbook.md` | Markdown | Human-readable skillbook grouped by section |
| `ace_processed.txt` | Text | Tracks which sessions have been processed |

The agent loads strategies by **reading `ace_skillbook.md` at session start**. This must be an explicit instruction in AGENTS.md β€” OpenClaw does not auto-inline linked files. The agent uses its file-reading tools to load the content into its context window.



Once loaded, the agent can cite strategy IDs (e.g., `conversation_style-00003`) when applying them.



---



## Example Skillbook Output



After processing a few sessions, `ace_skillbook.md` might contain:



```markdown

## conversation_style



### `conversation_style-00003`



Maintain brief, natural responses without performative language



**Justification:** Establishes consistent conversational tone across interaction types

**Evidence:** Maintained direct, helpful tone across greeting, creative request,

modification, and casual follow-up



*Tags: helpful=5, harmful=0, neutral=0*



## debugging



### `debugging-00005`



Test litellm calls directly before debugging ace pipeline



**Justification:** Systematic debugging approach that isolated authentication issues

**Evidence:** Direct litellm.completion() calls worked while ace failed



*Tags: helpful=1, harmful=0, neutral=0*

```



---



## Reference



### Environment variables



| Variable | Default | Description |

|---|---|---|

| `ACE_MODEL` | `bedrock/us.anthropic.claude-sonnet-4-20250514-v1:0` | LLM for reflection and skill extraction |

| `OPENCLAW_AGENT_ID` | `main` | Agent ID for session discovery |

| `OPENCLAW_HOME` | `$HOME/.openclaw` | OpenClaw home directory (used by `ace-learn` only) |

| `LITELLM_API_KEY` | β€” | API key (for non-Bedrock providers) |

| `SPH_LITELLM_KEY` | β€” | Alternative API key variable |

| `AWS_BEARER_TOKEN_BEDROCK` | β€” | AWS Bedrock bearer token |

| `ANTHROPIC_API_KEY` | β€” | Anthropic API key |

| `OPENROUTER_API_KEY` | β€” | OpenRouter API key |



### CLI arguments



```

ace-learn [OPTIONS] [FILES...]



Options:

  --dry-run          Parse sessions but skip learning (no LLM calls)

  --reprocess        Ignore processed log, reprocess all sessions

  --agent AGENT_ID   OpenClaw agent ID (default: main)

  --output DIR       Output directory for skillbook files

  --opik             Enable Opik observability logging



Positional:

  FILES              Specific JSONL files to process (skips discovery)

```



### Pipeline steps



**LoadTracesStep** β€” Reads a JSONL file and parses each line into a list of event dicts.



**OpenClawToTraceStep** β€” Converts raw OpenClaw events into a structured trace:



```python

{

    "question": "User: ...\n\nUser: ...",

    "reasoning": "[thinking] ...\n[tool:read] ...\n[response] ...",

    "answer": "Last assistant response",

    "skill_ids": [],

    "feedback": "OpenClaw session: 3 user messages, 1 assistant responses, model: ..., 14605 tokens",

    "ground_truth": None

}

```



**TraceAnalyser** β€” Runs the ACE learning tail:



1. **Reflect** β€” LLM analyzes the trace for patterns, errors, and effective strategies

2. **Tag** β€” Scores cited skills as helpful/harmful/neutral

3. **Update** β€” LLM decides skillbook mutations (ADD, UPDATE, REMOVE, CONSOLIDATE)

4. **Apply** β€” Commits changes to the in-memory skillbook



---



## Troubleshooting



??? question "Sessions directory not found"

    The agent hasn't completed a session yet, or `OPENCLAW_AGENT_ID` is wrong. Check:



    ```bash

    ls ~/.openclaw/agents/

    ```



??? question "Nothing new to learn from"

    All sessions have been processed. Use `--reprocess` to rerun, or wait for new sessions.



??? question "`ace-learn` not found in Docker"

    Make sure you built with `Dockerfile.ace` and are using the correct image tag:



    ```bash

    docker run --rm openclaw:local which ace-learn

    ```



??? question "Import errors for `ace` (host setup)"

    The Docker image includes a cloned copy of the ACE repo at `/opt/ace` with all dependencies pre-installed β€” this is handled by `Dockerfile.ace`. For the host setup, make sure you run from the ACE repo root with `uv run` so that `ace` is importable:



    ```bash

    cd /path/to/agentic-context-engine

    uv run python ~/.openclaw/workspace/skills/kayba-ace/learn_from_traces.py

    ```



??? question "API key errors in Docker"

    Make sure your LLM API key is passed through `docker-compose.yml`. Check with:



    ```bash

    docker compose exec openclaw-gateway env | grep -E 'API_KEY|BEARER_TOKEN|ACE_MODEL'

    ```



---



## What to Read Next



- [Integration Pattern](../guides/integration.md) β€” how the INJECT/EXECUTE/LEARN pattern works

- [The Skillbook](../concepts/skillbook.md) β€” how learned strategies are stored

- [ACE Design](../ACE_DESIGN.md) β€” architecture and step reference