Spectron is not limited to conversational memory. Its entity-attribute model and temporal validity system make it a natural fit for tracking the state of long-running, multi-step agent workflows – processes that span hours or days, survive restarts, and need to avoid repeating completed steps.
Why use Spectron for workflow state
Traditional approaches to workflow state – Redis keys, database records, task queue metadata – require purpose-built state management. Spectron adds:
Scoped isolation – workflow state for project A cannot interfere with project B.
Temporal validity – step results can expire, triggering re-execution.
Provenance – every state change traces back to the turn or operation that caused it.
State diff – query what changed since a checkpoint without building your own diffing logic.
Observability – inspect the full state of any workflow at any point in time.
Step 1 – Design your state model
Map your workflow steps to entity types and attributes. A research workflow might look like:
Entity type
Example entities
Key attributes
ResearchTask
market_analysis_q1
status, assigned_model, deadline
SearchResult
result_001
query, url, summary, relevance_score
Report
market_report_draft
status, word_count, sections_complete
Step 2 – Scope workflow state to a project identifier
Use a project or task scope dimension to isolate each workflow's state:
# Record the step as an agent turn – extraction captures the results awaitsession.turn( role="assistant", content=f"Completed search for '{query}'. Found {len(results)} results. " f"Top result: {results[0]['url']} – {results[0]['summary'][:200]}", )
awaitsession.turn({ role:"assistant", content:`Completed search for '${query}'. Found ${results.length} results. ` +`Top result: ${results[0].url} – ${results[0].summary.slice(0,200)}`, });
returnresults; }
Step 4 – Check state before each step
Before running a step, check whether it has already been completed. This makes the workflow idempotent – safe to restart without duplicating work.
asyncdefshould_run_step(memory,scope: dict,step_name: str) -> bool: ctx=awaitmemory.context( scope=scope, query=f"Has the {step_name} step been completed?", top_k=3, )
# Check if there is an existing completion record for this step foriteminctx.items: ifitem.entity_type=="WorkflowStep"anditem.attributes.get("name")==step_name: ifitem.attributes.get("status")=="completed": returnFalse
returnTrue
asyncfunctionshouldRunStep( memory:Memory, scope:Record<string,string>, stepName:string, ):Promise<boolean>{ const ctx =awaitmemory.context({ scope, query:`Has the ${stepName} step been completed?`, topK:3, });
Some workflow steps have results that go stale – a price lookup, a news summary, a resource availability check. Set valid_until on the turn to give the extracted attributes a time-to-live:
POST /api/v1/{context_id}/sessions/{session_id}/turns Content-Type: application/json