Demo

Ask questions about BundleLLM using BundleLLM. Connect your AI provider to try it.

SDK Reference

BundleLLM.init(config?)

Creates a new BundleLLM instance.

const ai = BundleLLM.init({ siteId: 'my-site' })
ParameterTypeDescription
config.apiUrlstring (optional)OAuth redirect handler URL
config.siteIdstring (optional)Site identifier for analytics (auto-derived from domain if omitted)
config.sessionTTLnumber (optional)Session TTL in seconds. Stored credentials expire after this duration. No default (credentials persist indefinitely). Negative values are ignored.

Returns: BundleLLMInstance


Instance Methods

renderSignIn(selector)

Renders a provider picker in the target element. Shows OpenRouter (OAuth) and Anthropic (API key) with model selection.

ai.renderSignIn('#sign-in-container')

renderChat(selector, options?)

Renders a complete chat widget with provider picker, model selector, streaming, token usage, and sign-out.

ai.renderChat('#chat', {
  placeholder: 'Ask anything...',
  context: 'Page content here...',
  welcomeMessage: 'Connect your AI to start.',
  theme: 'light',
  markdown: true,
})
OptionTypeDefaultDescription
placeholderstring"Ask something..."Input placeholder
contextstringSystem prompt sent with every message
welcomeMessagestring"Connect your AI to start chatting."Shown before sign-in
theme"light" | "dark""light"Color theme
markdownbooleantrueRender markdown in responses. Set false for plain text.

See Chat Widget for full options.


chat(request)

Starts a streaming chat. Returns a ChatStream.

const stream = ai.chat({
  messages: [{ role: 'user', content: 'Hello' }],
  context: 'Optional system prompt...',
  model: 'anthropic/claude-sonnet-4',
  maxTokens: 4096,
  temperature: 0.7,
})
ParameterTypeDescription
messagesArrayConversation history with role and content
contextstring (optional)Sent as system prompt with every message
modelstring (optional)Model override; uses selected model if omitted
maxTokensnumber (optional)Maximum response tokens (default: 4096)
temperaturenumber (optional)Sampling temperature

Returns: ChatStream


setContext(context?)

Updates the system prompt context dynamically. For renderChat, this overrides the initial context option. For direct chat() calls, this is used as a fallback when request.context is not provided. Pass undefined to clear the override and revert to the original context.

// Update context (e.g., when page content changes)
ai.setContext('Updated page content...')

// Clear the override
ai.setContext(undefined)
ParameterTypeDescription
contextstring | undefinedNew system prompt context, or undefined to clear

Context exceeding 10,000 characters is truncated with a console warning.


getStatus()

Returns the current connection status.

const status = ai.getStatus()
// { connected: true, provider: 'anthropic', model: 'claude-sonnet-4-20250514' }

Returns: { connected: boolean, provider?: string, model?: string }


setModel(modelId)

Changes the active model while connected. Persists to localStorage.

ai.setModel('anthropic/claude-haiku-4')

getModels()

Returns available models for the connected provider.

const models = ai.getModels()
// [{ id: 'claude-sonnet-4-20250514', name: 'Claude Sonnet 4' }, ...]

Returns: Array<{ id: string, name: string }>


disconnect()

Signs out the user’s AI provider. Clears stored connection.

ai.disconnect()

on(event, callback)

Listens for events.

ai.on('connected', ({ provider, model }) => { ... })
ai.on('disconnected', () => { ... })
ai.on('error', ({ message }) => { ... })
EventCallback DataDescription
connected{ provider, model }User connected a provider
disconnected.User signed out
error{ message }Error occurred

off(event, callback)

Removes an event listener.


destroy()

Tears down the instance completely. Removes rendered widget and sign-in DOM elements, clears the connection, removes event listeners, and cleans up internal state. All subsequent method calls on the instance will no-op. chat() will emit an "Instance has been destroyed" error.

ai.destroy()

ChatStream

Returned by ai.chat(). Supports chained .on() calls.

stream
  .on('start', ({ messageId }) => { ... })
  .on('delta', (text) => { ... })
  .on('done', ({ usage }) => { ... })
  .on('error', ({ message }) => { ... })
EventCallback DataDescription
start{ messageId }Stream started
deltastringText chunk received
done{ usage? }Stream complete; usage has inputTokens and outputTokens
error{ message }Stream error

stream.cancel()

Cancels the active stream.

stream.cancel()

Utilities

BundleLLM.renderMarkdown(text)

Converts markdown text to styled HTML. Available for custom UI integrations where you want the same formatting as the drop-in widget.

stream.on('delta', (text) => {
  fullText += text
  messagesEl.innerHTML = BundleLLM.renderMarkdown(fullText)
})

Supports: code blocks (with language labels), inline code, bold, italic, headers, ordered/unordered lists, and links. HTML in the input is escaped. Links are restricted to http: and https: protocols.

Returns: string (HTML)