|
| 1 | +# Session-as-checkpoint-resume |
| 2 | + |
| 3 | +**Problem.** How do I keep multi-turn agent state across turns? |
| 4 | + |
| 5 | +## Approach |
| 6 | + |
| 7 | +The framework's [checkpointing](../concepts/checkpointing.md) |
| 8 | +provides single-invocation crash resume out of the box. Multi-turn |
| 9 | +state is the same primitive used differently: the application |
| 10 | +keeps a stable `session_id → invocation_id` mapping, and each |
| 11 | +turn calls `invoke(resume_invocation=<prior_invocation_id>)` to |
| 12 | +pick up where the previous turn left off. |
| 13 | + |
| 14 | +The checkpointer returns the prior state. The new turn proceeds |
| 15 | +from there. Session-context fields that accumulate across turns |
| 16 | +(message history, retrieved facts, running totals) use a `merge` |
| 17 | +or `append` reducer so each turn's contribution adds to what's |
| 18 | +already there rather than replacing it. |
| 19 | + |
| 20 | +Each resume mints a new `invocation_id`; the `session_id` is the |
| 21 | +join key the application maintains, typically as the |
| 22 | +`correlation_id` on `invoke()` (which is preserved unchanged |
| 23 | +across resume). |
| 24 | + |
| 25 | +## Snippet |
| 26 | + |
| 27 | +```python |
| 28 | +from typing import Annotated |
| 29 | +from openarmature.checkpoint import SQLiteCheckpointer |
| 30 | +from openarmature.graph import END, GraphBuilder, State, append, merge |
| 31 | + |
| 32 | + |
| 33 | +class SessionState(State): |
| 34 | + messages: Annotated[list[dict], append] = [] |
| 35 | + facts: Annotated[dict[str, str], merge] = {} |
| 36 | + last_user_input: str = "" |
| 37 | + |
| 38 | + |
| 39 | +# ... define nodes that read s.messages, append to s.messages, |
| 40 | +# and merge into s.facts ... |
| 41 | + |
| 42 | +checkpointer = SQLiteCheckpointer(db_path="./sessions.db") |
| 43 | +graph = ( |
| 44 | + GraphBuilder(SessionState) |
| 45 | + .add_node("plan", plan) |
| 46 | + .add_node("respond", respond) |
| 47 | + .add_edge("plan", "respond") |
| 48 | + .add_edge("respond", END) |
| 49 | + .set_entry("plan") |
| 50 | + .with_checkpointer(checkpointer) |
| 51 | + .compile() |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +# The application maintains its own session table mapping |
| 56 | +# session_id -> latest invocation_id. OA's checkpointer doesn't |
| 57 | +# know about sessions; the join is the application's |
| 58 | +# responsibility. The session_id doubles as correlation_id so |
| 59 | +# observability traces share the cross-turn join key. |
| 60 | +async def handle_turn(session_id: str, user_input: str) -> str: |
| 61 | + initial = SessionState(last_user_input=user_input) |
| 62 | + prior_invocation_id = sessions_db.get_invocation_id(session_id) |
| 63 | + |
| 64 | + if prior_invocation_id is None: |
| 65 | + final = await graph.invoke(initial, correlation_id=session_id) |
| 66 | + else: |
| 67 | + final = await graph.invoke( |
| 68 | + initial, resume_invocation=prior_invocation_id |
| 69 | + ) |
| 70 | + |
| 71 | + # Record the new invocation_id for next turn's resume. |
| 72 | + # Read it from the checkpointer's latest record for this |
| 73 | + # correlation_id; exact lookup is application-side bookkeeping. |
| 74 | + sessions_db.set_invocation_id(session_id, latest_for(session_id)) |
| 75 | + |
| 76 | + return final.messages[-1]["content"] |
| 77 | +``` |
| 78 | + |
| 79 | +`sessions_db` is your application's session-state store (Postgres, |
| 80 | +Redis, a flat file, whatever); the checkpointer holds the OA-side |
| 81 | +state and the session table holds the join keys. |
| 82 | + |
| 83 | +## When this is the right pattern |
| 84 | + |
| 85 | +- Your application has long-lived sessions with multiple LLM turns |
| 86 | + and you want the prior state to be the starting point of the |
| 87 | + next turn. |
| 88 | +- You're already running a checkpointer for crash resume — this |
| 89 | + pattern is "use it more." |
| 90 | +- Cross-turn state has clean reducer semantics: `merge` for |
| 91 | + accumulating dicts, `append` for growing lists. |
| 92 | + |
| 93 | +## When it isn't |
| 94 | + |
| 95 | +- A session's "state" is bigger than fits comfortably in a single |
| 96 | + graph state shape. Split into multiple graphs and share an |
| 97 | + external store keyed by session. |
| 98 | +- Turns are completely independent — there's no value in carrying |
| 99 | + state across them. Then just run each turn as a fresh invoke. |
| 100 | +- The application already has its own state-management layer that |
| 101 | + conflicts with OA's frozen-state model. Use OA per-turn without |
| 102 | + cross-turn resume. |
| 103 | + |
| 104 | +## Cross-references |
| 105 | + |
| 106 | +- [Checkpointing](../concepts/checkpointing.md) — backend wiring, |
| 107 | + `resume_invocation`, schema migration. |
| 108 | +- [State and reducers](../concepts/state-and-reducers.md) — `merge` |
| 109 | + and `append` reducer strategies. |
| 110 | +- [`examples/08-checkpointing-and-migration`](../examples/08-checkpointing-and-migration.md) — |
| 111 | + single-resume baseline. |
| 112 | +- Spec: [pipeline-utilities](https://openarmature.org/capabilities/pipeline-utilities/) |
0 commit comments