reflowkit
v0.0.3
Published
Reactive recovery for LLM API calls — compact-and-retry, strip-and-retry, and truncation recovery.
Readme
Reflow (TypeScript)
Part of LoopEngine — a runtime
for defining and running AI agents through a transparent ReAct loop —
handling retries for prompt-too-long / truncated-output model failures.
Published to npm as reflowkit. Works standalone too, no dependency on
LoopEngine itself.
See the root README for the pitch, the prior-art table, and the scope decisions. This file only covers what's specific to running the code.
Install
npm installQuickstart
npm run quickstartimport { Reflow } from './src/index.js'
import { ContextClipper } from 'contextclip'
const contextClip = new ContextClipper({ budgetTokens: 180000 })
const reflow = new Reflow<Message[]>({
onPromptTooLong: async (messages) => (await contextClip.recover(messages)).messages,
onMediaTooLarge: (messages) => stripLargeImages(messages),
onTruncated: async (messages) => [...messages, { role: 'user', content: 'continue' }],
maxTruncationRetries: 2,
})
const result = await reflow.call(
(messages) => anthropic.messages.create({ model, messages, max_tokens: 4096 }),
initialMessages,
)
// result.value — the response
// result.recoveries — ordered log of what happened, e.g. ['prompt_too_long']
// result.truncated — true if still truncated after exhausting retriesTest / build
npm test # vitest
npm run build # tsc -> dist/Status
Prompt-too-long recovery, media-too-large recovery, and bounded
truncation recovery are real and tested, with default classifiers for
common Anthropic/OpenAI-shaped errors. Published as
reflowkit on npm — the bare
reflow name has unrelated squats on both registries.
