Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

uji.context

These functions control what the model sees besides the conversation. They add your own lines to each turn and decide when uji compacts old messages.

uji.context.add(name, provide, opts)

Registers a function that uji calls at the start of every turn. What it returns decides where the text goes.

ReturnEffect
a stringuji appends it to the system prompt.
{ text = "...", at = "turn" }uji adds the text after your message and keeps it in the conversation. The transcript does not show it.
niluji adds nothing this turn.

Text added with at = "turn" stays in the conversation. When it stops being true, return a line that says so once, such as Plan mode is off. Return text that changes between turns this way, not as a string. When the system prompt changes, the provider cannot reuse its cache for the conversation.

opts.priority orders the functions, lowest first. The default is 50. A function with the same name replaces the earlier one.

uji.context.add("branch", function()
  local branch = io.popen("git branch --show-current"):read("*l")
  if branch and branch ~= "" then
    return "The current git branch is " .. branch .. "."
  end
end, { priority = 20 })

uji.context.remove(name)

Removes a context function and returns true if it existed.

uji.context.remove("branch")

uji.context.list()

Returns the names of the context functions, in the order uji calls them.

local names = uji.context.list()

uji.context.configure(opts)

Sets how long the provider caches the conversation, and when uji compacts. When the conversation grows past the model’s context window minus a reserve, uji summarises older messages before the next turn.

KeyMeaningDefault
cacheHow long the provider keeps the conversation cached between turns. "off", "short" for 5 minutes, or "long" for 1 hour. A cache write costs more with "long", and the cache survives longer pauses. It applies to models with cache = true in uji.provider.add."short"
compaction.enabledCompact automatically. /compact works either way.true
compaction.reserveTokens to keep free for the reply.the model’s output limit, or 20,000 when unknown, at most a quarter of the window
compaction.keep_recentTokens of recent messages to keep word for word.20000

Raises an error for an unknown key or an unknown cache value.

uji.context.configure({
  cache = "long",
  compaction = { keep_recent = 40000 },
})