Context Providers
Why
A common agent shape: “answer using these N markdown docs / this DB row / today’s status feed”. You can do that with tools (the LLM callsfetch_status() whenever it needs the data), but for context that the LLM always needs, tools are wasteful:
- An extra round-trip per turn.
- The LLM might forget to call.
- Tool definitions take prompt space.
ContextProvider does.
ContextProvider interface
Built-in providers
FilesystemContextProvider
glob under basePath, concatenates them, returns the result. Files over maxFileSize are skipped. Total file count capped at maxFiles.
Each file is prefixed with # <relative path> so the model knows what came from where.
Use for: project README, design docs, prompt templates loaded from disk, user-uploaded notes.
HttpContextProvider
fetch() call unless cacheTtlMs > 0, in which case the result is cached for that many milliseconds.
If the request fails (network error, non-2xx), the provider returns null and silently continues. No retries (wrap with your own if you need them).
Use for: status feeds, configuration endpoints, “latest pricing” data.
DatabaseContextProvider
ContextProvider interface, with the name used for the <context> tag.
Use for: anything that’s not a file or HTTP endpoint — DBs, cache, in-process Maps, custom APIs.
Plugging into an Agent
instructions resolver is on the roadmap; for now, fetch the context and inject it yourself.)
resolveContextProviders(providers, query, ctx)
The helper that runs all providers in parallel and concatenates their outputs.
- Runs
fetch()on every provider in parallel viaPromise.allSettled. - Skips providers that throw or return
null. - Wraps each non-null result in
<context name="...">...</context>. - Returns the joined string (or
""if nothing returned).
Caching strategies
HttpContextProvider has built-in TTL caching. For the others, layer it yourself:
Per-user context
ctx carries userId, tenantId, sessionState. Use them inside fetch() to personalize:
RunContext that the agent sees, so any state you’ve stashed via RunContext.sessionState is available.
Token cost considerations
Context is paid for on every turn. A 50KB markdown corpus injected into the system prompt costs ~$0.03 per turn at GPT-4o pricing.- Always-on, small: context provider is the right choice.
- Sometimes-on, large: use a tool (
searchNotes) or RAG retrieval instead. - Always-on, large: consider prompt caching (Anthropic’s
cache_control, OpenAI’sprompt_caching) — Agentium honors both when the underlying provider supports them.
See also
- Prompts and Instructions — how the resolved context gets composed into the system message
- GraphRAG — when “context” is a graph query
- Skills — the skill system is itself a kind of context provider (with extra structure)