Save and restore

Call checkpoint() between turns and store the returned bytes. This complete example saves one exchange to conversation.fx, then continues it in a new agent:

import { createFxAgent } from 'libfx'
import { writeFile, readFile } from 'node:fs/promises'

const options = { apiKey: process.env.AI_GATEWAY_API_KEY }
const agent = await createFxAgent(options)
try {
  const turn = agent.prompt('Explain a database index in one sentence.')
  for await (const _ of turn) {}
  await turn.result
  await writeFile('conversation.fx', await agent.checkpoint())
} finally {
  await agent.close()
}

const restored = await createFxAgent({
  ...options,
  checkpoint: await readFile('conversation.fx'),
})
try {
  const turn = restored.prompt('Give me an example of that.')
  for await (const event of turn) {
    if (event.type === 'text_delta') process.stdout.write(event.delta)
  }
  await turn.result
} finally {
  await restored.close()
}

A checkpoint contains conversation history and usage. Restore it into a new agent and supply credentials, model selection, instructions, and tools again. They are not saved in the checkpoint.