Python application programming interface reference¶
An application programming interface (API) defines how software components communicate. Import this public API from library_of_context or context_cache.
Mkdocstrings generates the reference sections from the source code.
Model vocabulary¶
- A
ContextEventis an ordered source item in one governed thread. It can contain a message, instruction, or tool result. - A
ContextRecordis a searchable unit in therecordstable. It contains text, an embedding, metadata, origin data, and a visibility scope. - A book is the public name and serialized view of one
ContextRecord. The Library has no separateBookclass or table. - An event reserves the identifier for its derived record. A direct record write cannot replace that searchable copy.
- A
ThreadKey(collection, session_id)identifies one stateful chat. The collection identifies the project, and the session identifier identifies the thread. - A
WorkingSetis the size-limited reading-desk snapshot. The Library assembles it from records that the caller can access.
Agent-facing transport views¶
Model Context Protocol (MCP) tools use size-limited transport views for searches and reading desks. Hypertext Transfer Protocol (HTTP) routes use the same views.
A search result contains scores, a small record reference, and a short excerpt. Treat the excerpt as untrusted data.
A desk contains the size-limited context block and small search-result references. The context block is ready for a model prompt.
These transport views omit embeddings, complete metadata, and complete record text. An embedding is a numeric representation that supports similarity searches.
Commit and protect operations return size-limited event acknowledgements. They do not return the submitted content or metadata.
Direct record administration routes return complete records.
A daemon is a background process that owns shared Library resources. The daemon versions its MCP message format separately from its SQLite database format.
A bridge rejects a daemon that uses a different message version.
Storage path¶
The Library requires a SQLite database file. This file supports recovery and exclusive ownership by one Library runtime.
The Library does not support the SQLite :memory: value.
Text-agent adapter¶
context_cache.agent.GovernedTextAgent
¶
Wrap a stateless text-model callback with bounded prompt assembly and persistence.
The supplied callback must send exactly the messages it receives. It must not
append another transcript or continue a provider-managed conversation. Structured
tool calls, streaming deltas, attachments, and multimodal values need a custom
adapter rather than this text-only interface.
Source code in context_cache/agent.py
turn
¶
turn(
user_message: str,
*,
turn_id: str | None = None,
focus: str | None = None,
protected: bool = False,
strict_freshness: bool = False,
) -> str
Run one governed text turn and durably record the returned response.
Pass a stable, unique turn_id when the caller may retry. The governor then
uses deterministic user and assistant event IDs for idempotent persistence.
Source code in context_cache/agent.py
Context governor¶
context_cache.governor.LibraryContextGovernor
¶
Own the durable record -> bounded prompt -> durable response lifecycle.
The model receives a bounded prompt assembled for every call. Full thread events stay in SQLite, recent events remain immediately visible through an in-memory overlay, and a bounded work ring indexes durable outbox events asynchronously.
Source code in context_cache/governor.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
record
¶
record(
role: Role,
content: str,
*,
metadata: dict[str, Any] | None = None,
importance: float | None = None,
protected: bool | None = None,
event_id: str | None = None,
) -> ContextEvent
Durably append one thread event and queue it for asynchronous indexing.
System and developer roles are protected by default. A caller-supplied
event_id makes an identical retry idempotent; reusing it with different
event fields fails.
Source code in context_cache/governor.py
prepare
¶
prepare(
user_message: str,
*,
focus: str | None = None,
system_prompt: str = "",
metadata: dict[str, Any] | None = None,
importance: float = 0.5,
protected: bool = False,
event_id: str | None = None,
strict_freshness: bool = False,
) -> GovernedPrompt
Durably record a user turn, then construct its bounded model request.
Source code in context_cache/governor.py
commit
¶
commit(
content: str,
*,
role: Role = "assistant",
metadata: dict[str, Any] | None = None,
importance: float | None = None,
protected: bool | None = None,
event_id: str | None = None,
) -> ContextEvent
Record the model or tool result after a governed model call.
Source code in context_cache/governor.py
protect
¶
protect(
content: str,
*,
role: Role = "developer",
label: str | None = None,
importance: float = 1.0,
event_id: str | None = None,
) -> ContextEvent
Append critical context that remains eligible until explicitly released.
Source code in context_cache/governor.py
release
¶
Remove protection without deleting the durable event or indexed record.
Source code in context_cache/governor.py
build_prompt
¶
build_prompt(
*,
focus: str | None = None,
system_prompt: str = "",
strict_freshness: bool = False,
) -> GovernedPrompt
Build a bounded envelope from protected, recent, and retrieved context.
Set strict_freshness only when the caller must wait for asynchronous index
visibility. Normal interactive calls use the recent overlay instead.
Source code in context_cache/governor.py
flush
¶
Wait until the thread's outbox has reached its indexed watermark.
Source code in context_cache/governor.py
retry_failed
¶
Return a quarantined event to the indexing queue.
Source code in context_cache/governor.py
status
¶
Return visibility watermarks, ring pressure, and worker health.
Library facade¶
context_cache.library.LibraryOfContext
¶
Bases: ContextCache
Public API for context storage, retrieval, reading desks, and governed prompts.
Source code in context_cache/library.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
shelve
¶
shelve(
text: str,
*,
book_id: str | None = None,
collection: str | None = None,
catalog: dict[str, Any] | None = None,
source: str = "manual",
importance: float = 0.5,
shelf_life_seconds: float | None = None,
scope: ContextScope | str = ContextScope.PROJECT,
owner_session_id: str | None = None,
team_id: str | None = None,
) -> ContextRecord
Source code in context_cache/library.py
shelve_document
¶
shelve_document(
text: str,
*,
source: str,
collection: str | None = None,
catalog: dict[str, Any] | None = None,
importance: float = 0.5,
chapter_tokens: int = 450,
overlap_tokens: int = 60,
replace_edition: bool = False,
scope: ContextScope | str = ContextScope.PROJECT,
owner_session_id: str | None = None,
team_id: str | None = None,
) -> list[ContextRecord]
Source code in context_cache/library.py
consult
¶
consult(
subject: str,
*,
max_books: int = 8,
collection: str | None = None,
catalog_filters: dict[str, Any] | None = None,
minimum_relevance: float = 0.0,
team_ids: tuple[str, ...] = (),
) -> list[SearchHit]
Source code in context_cache/library.py
promote_book
¶
promote_book(
book_id: str,
*,
target_scope: ContextScope | str,
collection: str | None = None,
source_session_id: str | None = None,
promoted_book_id: str | None = None,
target_team_id: str | None = None,
) -> ContextRecord
Source code in context_cache/library.py
open_reading_desk
¶
open_virtual_session
¶
open_virtual_session(
session_id: str,
*,
collection: str | None = None,
token_budget: int = 12000,
recent_token_budget: int = 4000,
) -> "VirtualContextSession"
Source code in context_cache/library.py
Thread identity and visibility¶
context_cache.scopes.ThreadKey
dataclass
¶
Stable identity for one agent thread inside a collection.
Source code in context_cache/scopes.py
context_cache.scopes.ContextScope
¶
context_cache.scopes.ScopeSelection
dataclass
¶
Authorized record scopes for one retrieval operation.
Source code in context_cache/scopes.py
Shared runtime configuration¶
context_cache.runtime.RuntimeSettings
dataclass
¶
Bounded process-level worker and desk resources.
Source code in context_cache/runtime.py
context_cache.runtime.LibraryRuntime
¶
Own the bounded indexing and desk services for one Library process.
Source code in context_cache/runtime.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
Loopback daemon client¶
Every daemon route requires Authorization: Bearer <token>. A bearer token is a secret value that grants access to its holder.
An HTTP POST request sends a body to a route. An HTTP GET request reads data from a route.
The serve command reads or creates the owner-readable file from --auth-token-file. The default path is <database-path>.daemon-token.
A thin MCP bridge reads the same file through --daemon-token-file.
LibraryDaemonClient requires the token in its bearer_token argument. It accepts only loopback HTTP addresses and sends the token with each request.
A loopback address sends traffic only inside the local computer. The daemon rejects browser origins, cross-site requests, and non-loopback Host values.
The daemon also rejects POST bodies unless their media type is application/json. JavaScript Object Notation (JSON) is the daemon message format.
context_cache.client.LibraryDaemonClient
¶
Call one Library daemon over its dependency-free loopback HTTP protocol.
Source code in context_cache/client.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
Reading desk¶
context_cache.library.ReadingDesk
¶
Token-bounded active context whose contents are replaced during refresh.
Source code in context_cache/library.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
lay_out
¶
lay_out(
subject: str,
*,
session_id: str,
token_budget: int = 4000,
max_books: int = 12,
namespace: str | None = None,
catalog_filters: dict[str, Any] | None = None,
keep_open: list[str] | None = None,
leave_shelved: list[str] | None = None,
team_ids: tuple[str, ...] = (),
) -> WorkingSet
Source code in context_cache/library.py
change_subject
¶
current_books
¶
Core models¶
context_cache.models.ContextEvent
dataclass
¶
One durable event in a governed agent thread.
Source code in context_cache/models.py
context_cache.models.ContextWatermarks
dataclass
¶
Durable visibility boundaries for a governed thread.
Source code in context_cache/models.py
context_cache.models.GovernedPrompt
dataclass
¶
A complete bounded model request assembled by the context governor.