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