Skip to main content

Context, IDs, and background tasks

This concept covers three runtime utilities that support larger graph systems:

  • Context managers shape what history is sent to the model.
  • ID generators control generated identifiers.
  • Background task and shutdown utilities keep async side work under control.

Context managers

MessageContextManager controls the message history sent to the model without deleting persisted checkpointer history.

Use it for token budget control, trimming old messages, preserving important system or tool messages, and custom context policies.

from agentflow.core import StateGraph
from agentflow.core.state import MessageContextManager

graph = StateGraph(
context_manager=MessageContextManager(max_messages=20),
)

Context management changes model input, not the durable thread state stored by a checkpointer.

ID generators

ID generators are passed to StateGraph when you need a specific ID style.

Common generators include:

GeneratorUse case
DefaultIDGeneratorFramework default.
UUIDGeneratorGlobally unique string IDs.
BigIntIDGeneratorLarge integer IDs.
TimestampIDGeneratorTime-based IDs.
ShortIDGeneratorCompact generated IDs.
from agentflow.core import StateGraph
from agentflow.utils import UUIDGenerator

graph = StateGraph(id_generator=UUIDGenerator())

Changing ID format can affect API and client assumptions, so verify serialized types when changing this in a public app.

Background tasks

BackgroundTaskManager manages async side work such as non-blocking memory writes, cleanup, or telemetry.

Important operations include:

MethodPurpose
create_taskStart and track a task.
get_task_countInspect active tasks.
wait_for_allWait for tracked tasks to finish.
cancel_allCancel tracked tasks.
shutdownStop the manager cleanly.

Pair background tasks with graceful shutdown so streams, publishers, storage clients, and background writes can finish or cancel cleanly.