Skip to main content

Context Blocks

Context blocks are live data feeds for agent prompts. Before every agent request, registered context blocks run in parallel to fetch real-time data — CRM profiles, active tasks, user memory, knowledge base summaries — and inject it into the system prompt. The agent sees your live organizational context without you pasting it into every message.

How it works

  1. Context blocks are registered with the @context_block decorator, declaring which agent(s) they apply to
  2. Before a chat request, all blocks for the target agent run in parallel
  3. Each block returns a text string (or empty to skip)
  4. The rendered strings are injected into the agent’s system prompt as runtime_prompt_blocks
  5. Results are cached per-user with a 5-minute TTL to avoid redundant data fetches

Built-in context blocks

BlockSourceWhat it provides
CRM contextConnected CRMUser’s title, department, org hierarchy, rep management structure, CRM field mappings
TasksTask systemActive tasks, follow-ups, and due items for the current user
User memoryMemory servicePersistent memory — learned preferences, facts, and behavioral patterns distilled from past conversations
KnowledgeKnowledge basesSummaries of attached knowledge bases available for retrieval
PersonalizationUser settingsStyle/tone preferences, custom instructions, and about-user profile

Pre-warming

Context blocks can be pre-warmed before the user sends their first message. Your client calls the warm endpoint when the chat interface opens, caching all context so the first chat message doesn’t incur block-building latency.

SDK

agent = await af.Agent.get("MainAgent")

# Pre-warm context (call when user opens chat)
await agent.warm_context()

# First message is fast — context is already cached
response = await agent.run("What's my pipeline look like?")

REST API

POST /agent/context/warm

Listing and inspecting context blocks

SDK

agent = await af.Agent.get("MainAgent")

# List registered context blocks
blocks = await agent.list_context_blocks()
for block in blocks:
    print(f"{block['name']} -> agents: {block['agents']}")

# Build and inspect rendered context
rendered = await agent.build_context()
for name, content in rendered.items():
    print(f"--- {name} ---")
    print(content[:200])

REST API

# List registered context blocks for an agent
GET /agent/{agent_id}/context/blocks

# Build all context blocks and return rendered content
POST /agent/{agent_id}/context/blocks/build

Per-agent scoping

Context blocks declare which agents they apply to. The CRM context block might only run for MainAgent, while the tasks block runs for all agents. This ensures each agent only receives context relevant to its domain.

Entity resolution

In addition to background context blocks, AgentFlow supports inline entity resolution via context_refs on chat requests. When a user @mentions a CRM account or contact, the entity resolver fetches its data and injects it directly into the current turn:
POST /agent/{agent_id}/chat
{
  "message": "Prep me for my call with @Acme Corp",
  "context_refs": [
    { "system": "crm", "type": "account", "id": "001ABC123" }
  ],
  "stream": true
}
The referenced account’s profile, contacts, opportunities, and recent activity are resolved and included as context for that specific request.

Cache management

  • TTL: 5 minutes per user per agent
  • Max entries: 200 (LRU eviction when full)
  • Invalidation: updating user memory or settings automatically invalidates the cache
  • Per-execution snapshots: each agent execution gets its own snapshot, so sub-agents build independent context