Skip to content

createNeuron() โ€‹

Creates a Neuron instance โ€” the main interface for in-browser LLM chat.

ts
import { createNeuron } from '@agent-layer-zero/dendrite'

const neuron = createNeuron(config)

Model loading starts immediately on creation. Use onProgress to track download progress.

Config Options โ€‹

Required โ€‹

OptionTypeDescription
modelIdstringWebLLM model ID (e.g. 'gemma-2-2b-it-q4f16_1-MLC')

Performance (optional) โ€‹

OptionTypeDescription
workerWorker | () => WorkerOptional Web Worker for off-thread performance. See Worker Setup. If omitted, runs on main thread.

Personality (use one) โ€‹

OptionTypeDescription
systemPromptstringSimple system prompt string
personalityDocsPersonalityDoc[]Typed personality documents

Generation โ€‹

OptionTypeDefaultDescription
temperaturenumber0.2Randomness (0 = deterministic, 2 = creative)
maxTokensnumber2048Max tokens to generate per response
frequencyPenaltynumber0.5Penalty for repeated tokens (reduces loops)
maxHistoryTurnsnumber10How many conversation turns to include

Caching โ€‹

OptionTypeDefaultDescription
useIndexedDBCachebooleantrueCache model weights in IndexedDB

Inference Mode โ€‹

By default Dendrite auto-selects the best engine for the device (WebGPU โ†’ WASM โ†’ API). Override this with inference or engine.

OptionTypeDefaultDescription
inference'auto' | 'fastest' | 'fallback' | 'api''auto'Selection strategy. 'auto' tries WebGPU then WASM then API. 'fastest' requires WebGPU, throws otherwise. 'fallback' forces Transformers.js (widest compatibility). 'api' requires apiUrl.
engine'webllm' | 'transformers' | 'api'โ€”Force a specific engine, overriding inference.

Callbacks โ€‹

OptionTypeDescription
onProgress(percent: number, text: string) => voidModel download progress (0-100)
onError(error: Error) => voidAny error (load, generation, etc.)
onLoadingChange(loading: boolean) => voidLoading state transitions
onGeneratingChange(generating: boolean) => voidGenerating state transitions
onInferenceSelected(tier: { engine, mode, reason }) => voidFires when the engine tier is chosen. engine is 'webllm' | 'transformers' | 'api', mode is 'webgpu' | 'wasm' | 'cloud'.

API Connection (optional) โ€‹

OptionTypeDescription
apiUrlstringAgentLayerZero API URL
usernamestringProfile username
instanceSlugstringInstance slug
apiTokenstringAuth token for private instances

Returns โ€‹

A Neuron instance.

Example โ€‹

ts
const neuron = createNeuron({
  modelId: 'gemma-2-2b-it-q4f16_1-MLC',
  personalityDocs: [
    { type: 'zero-shot', content: 'You are a pirate.' },
    { type: 'knowledge', content: 'You sailed the seven seas.' },
  ],
  temperature: 0.7,
  onProgress: (pct, text) => updateProgressBar(pct),
  onLoadingChange: (loading) => toggleSpinner(loading),
  onError: (err) => showError(err.message),
})

Part of the AgentLayerZero platform