You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user-guide/workflows/configuration/context-management.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -233,6 +233,7 @@ task: |
233
233
- ⚠️ **Simple lists**: Not added to context as variables (use output_key or wrap in dict)
234
234
- ⚠️ **Plain strings**: Not added to context as variables (use output_key or output as JSON)
235
235
- 💡 **Best practice**: Always output structured JSON from assistants for maximum flexibility
236
+
- 💡 **Parallel iterations**: Use `append_to_context: true` with `output_key` to collect all iteration results into a list — without it, only the last iteration's value is retained
236
237
237
238
#### Accessing Context in Templates
238
239
@@ -337,6 +338,40 @@ states:
337
338
include_in_llm_history: false # Don't show raw IDs to LLM
338
339
```
339
340
341
+
**append_to_context** (boolean, default: `false`)
342
+
343
+
Controls whether the current state's output is appended to an existing list in the context store rather than overwriting it.
344
+
345
+
- `true`: Output is accumulated — the value under `output_key` in the context store grows into a list across multiple executions (most useful with `iter_key` iterations)
346
+
- `false` (default): Output overwrites the previous value under the same key
347
+
348
+
**When to use `true`:**
349
+
350
+
- Collecting results from all parallel `iter_key` iterations into a single list
351
+
- Any scenario where multiple states write to the same key and all values must be preserved
352
+
353
+
**Requirements and constraints:**
354
+
355
+
- Use together with `output_key` to specify which context key accumulates the results
356
+
- When `append_to_context: true`, the top-level state key (set by `output_key`) is **not** written — read the accumulated list from the context store only via `{{output_key}}`
357
+
- Has no effect when `store_in_context: false`
358
+
359
+
```yaml
360
+
states:
361
+
- id: process-item
362
+
assistant_id: processor
363
+
task: "Analyze {{task}} and return a result object"
Clears all prior messages from the message history, creating a "fresh start" for the LLM context.
@@ -543,6 +578,18 @@ next:
543
578
544
579
Use case: When iterating over items, prevent context from accumulating across iterations. Each iteration gets only the current item, not data from previous iterations.
545
580
581
+
**Pattern 7: Accumulate All Iteration Results Into a List**
582
+
583
+
```yaml
584
+
next:
585
+
state_id: aggregate
586
+
iter_key: items
587
+
output_key: all_results
588
+
append_to_context: true
589
+
```
590
+
591
+
Use case: When iterating in parallel and you need to collect every branch's output into a single list. Without `append_to_context: true`, only the last branch's value is kept (last-value-wins). With it, `context_store["all_results"]` contains a list with one entry per iteration.
592
+
546
593
### 6.3 Dynamic Value Resolution
547
594
548
595
Dynamic value resolution enables you to use context store values throughout your workflow configuration using template syntax.
- When `true`, each iteration's output is **appended** to a list in the context store instead of overwriting the previous value
367
+
- When `false` (default), standard overwrite semantics apply — the last iteration's value wins on duplicate keys
368
+
- Use together with `output_key` to control which context key accumulates the collected results
369
+
- When `append_to_context: true` is combined with `output_key`, the top-level state key is **not** set — values are only available via the accumulated list in the context store
370
+
371
+
```yaml
372
+
next:
373
+
state_id: collect-results
374
+
iter_key: items
375
+
output_key: processed_items
376
+
append_to_context: true # Each iteration appends its output; context_store["processed_items"] becomes a list
377
+
```
378
+
364
379
#### Multi-Stage Iteration:
365
380
366
381
For multi-stage iteration (when you have multiple sequential states processing each item), the **same `iter_key` must be present in every state** included in the iteration chain.
@@ -550,6 +565,57 @@ states:
550
565
4. After all branches complete, contexts and message histories are merged
551
566
5. Merged results flow to `merge-results`
552
567
568
+
**Example 7: Accumulating Results Across Iterations**
569
+
570
+
When all iteration results must be preserved, use `append_to_context: true` so each parallel branch contributes to a shared list rather than overwriting it.
Create a severity report based on all ticket analyses.
600
+
Analyses: {{analyses}}
601
+
# Receives the full list of all three analyses in {{analyses}}
602
+
next:
603
+
state_id: end
604
+
```
605
+
606
+
**How it works:**
607
+
608
+
1. Three parallel branches process one ticket each
609
+
2. Each branch writes its output with the `analyses` key via `append_to_context: true`
610
+
3. The reducer appends each result to the list — no overwriting occurs
611
+
4. `create-report` receives `analyses = [result_1, result_2, result_3]` in its context
612
+
613
+
:::tip
614
+
`append_to_context: true` is the recommended way to collect results from all parallel iterations into a single list. It replaces the workaround of using unique per-iteration keys (`result_1`, `result_2`, ...).
615
+
:::
616
+
617
+
---
618
+
553
619
#### Context Isolation and Merging:
554
620
555
621
Iterations have important context management characteristics that ensure proper isolation and aggregation:
@@ -571,9 +637,17 @@ Iterations have important context management characteristics that ensure proper
571
637
572
638
- When all parallel iterations complete (fan-in), their context stores are **merged**
573
639
- The merge uses `add_or_replace_context_store` reducer
574
-
- For duplicate keys across iterations, the **last value wins** (last iteration overwrites previous)
640
+
- **Default (overwrite)**: for duplicate keys across iterations, the **last value wins** (last iteration overwrites previous)
641
+
- **Accumulation mode**: when `append_to_context: true` is set on the iterating state, each iteration's output is **appended** to a list under the specified key — no values are lost
575
642
- The merged context is then passed to the next state after iteration
576
643
644
+
**Choosing between overwrite and accumulation:**
645
+
646
+
| Mode | Config | Result for key `output` after 3 iterations |
- Similarly, message histories from all iterations are also merged
@@ -611,9 +685,9 @@ states:
611
685
**Important Context Merging Notes:**
612
686
613
687
- Iterations are isolated during execution but merged after completion
614
-
- Context keys set by multiple iterations will have only one final value (last wins)
615
-
- To preserve all iteration results, use unique keys (e.g., `result_1`, `result_2`) or aggregate into lists
616
-
- Message histories are fully preserved from all iterations
688
+
- By default, context keys set by multiple iterations will have only one final value (last wins)
689
+
- To preserve all iteration results, set `append_to_context: true` combined with `output_key` — the context key will accumulate all values as a list
690
+
- Message histories are fully preserved from all iterations regardless of the merge mode
617
691
618
692
#### Important Notes:
619
693
@@ -626,5 +700,7 @@ states:
626
700
- Cannot combine `iter_key` with `state_ids` (parallel transitions) or `condition`/`switch`
627
701
- Each iteration branch has isolated context and message history during execution
628
702
- After all iterations complete, contexts and message histories are merged using LangGraph reducers
703
+
- Use `append_to_context: true` to accumulate all iteration outputs into a list; without it, only the last iteration's value is retained for duplicate keys
704
+
- `append_to_context: true` has no effect when `store_in_context: false`
0 commit comments