Skip to content

LayerScale SDK

The official LayerScale SDK provides native access to sessions, streaming data ingestion, and Flash Queries. Use it for real-time streaming workloads where you need persistent sessions and continuous data processing.

For standard chat completions and tool calling, you can use the OpenAI or Anthropic SDKs instead. LayerScale is compatible with both.

Installation

Terminal window
npm install @layerscale/layerscale

Create a Session

import { LayerScale } from "@layerscale/layerscale";
const client = new LayerScale("http://localhost:8080", { apiKey: "LS-..." });
const session = await client.sessions.create({
type: "ohlcv",
prompt: "You are a financial analyst. Analyze incoming market data.",
flash: [
{ query: "Is the trend bullish or bearish?", max_tokens: 4 },
{ query: "What is the current support level?", max_tokens: 16 },
],
markPrefix: true,
});
console.log(`Session created: ${session.session_id}`);

The type parameter is required and determines the data format for the session. Supported types: ohlcv, iot, spatial, event, vitals.

The markPrefix / mark_prefix flag freezes the system prompt in the cache right after the session is created, so it is not reprocessed on every incoming data update.

Push Streaming Data

Push data points into the session. The endpoint is non-blocking: data is queued and processed in the background.

// Push OHLCV data
await client.sessions.push(session.session_id, [
{ o: 185.5, h: 186.2, l: 185.1, c: 185.8, v: 12500 },
{ o: 185.8, h: 186.5, l: 185.6, c: 186.3, v: 8700 },
]);
// Push IoT sensor data (on an "iot" session)
await client.sessions.push(session.session_id, [
{ sid: "temp-01", val: 72.5, lo: 60.0, hi: 85.0 },
]);
// Push vitals data (on a "vitals" session)
await client.sessions.push(session.session_id, [
{ hr: 72.0, bp_s: 120.0, bp_d: 80.0, spo2: 98.5, temp: 36.8 },
]);

Pass { wait: true } to block until the pushed items are fully processed:

await client.sessions.push(session.session_id, candles, { wait: true });

Query the Session

Generate a response based on all the data the session has seen:

// Blocking query
const result = await client.sessions.query(session.session_id, {
prompt: "What's the trend over the last hour?",
max_tokens: 200,
});
console.log(result.text);
// Streaming query
for await (const chunk of client.sessions.queryStream(session.session_id, {
prompt: "Describe the price action.",
max_tokens: 300,
})) {
if (!chunk.done) process.stdout.write(chunk.token);
}

Receive Flash Query Results

SSE (Server-Sent Events)

for await (const event of client.sessions.events(session.session_id)) {
if (event.type === "flash_ready") {
console.log(`Query ${event.id}: ${event.value} (confidence: ${event.confidence})`);
}
}

WebSocket

The client supports WebSocket for combined push and event delivery in a single connection:

const socket = client.sessions.stream(session.session_id);
socket.on("connected", (data) => {
console.log(`Connected: ${data.session_id}`);
});
// Push data
socket.push([{ o: 185.5, h: 186.2, l: 185.1, c: 185.8, v: 12500 }]);
// Listen for flash query results
socket.on("flash_ready", (event) => {
console.log(`${event.query}: ${event.value}`);
});
socket.on("error", (err) => {
console.error(`Error: ${err.message}`);
});

Stream Status and Stats

// Check streaming processor status
const status = await client.sessions.streamStatus(session.session_id);
console.log(`Queue: ${status.streaming.queue_size}/${status.streaming.queue_capacity}`);
console.log(`Processed: ${status.statistics.items_processed} items`);
// Get computed stats for the session's data
const stats = await client.sessions.stats(session.session_id);
console.log(stats);

Delete a Session

await client.sessions.delete(session.session_id);

Full Example

import { LayerScale } from "@layerscale/layerscale";
const client = new LayerScale("http://localhost:8080", { apiKey: "LS-..." });
const session = await client.sessions.create({
type: "ohlcv",
prompt: "Monitor ETH/USD for breakout signals.",
flash: [
{ query: "Is a breakout imminent?", max_tokens: 8 },
{ query: "What's the key resistance level?", max_tokens: 16 },
],
markPrefix: true,
});
// Connect via WebSocket for combined push + events
const socket = client.sessions.stream(session.session_id);
socket.on("flash_ready", (event) => {
console.log(`[FLASH] ${event.query}: ${event.value} (${event.confidence})`);
});
socket.on("open", () => {
// Push data from your feed
setInterval(() => {
const candle = getLatestCandle();
socket.push([{
o: candle.open,
h: candle.high,
l: candle.low,
c: candle.close,
v: candle.volume,
}]);
}, 1000);
});

When to Use Which Client

Use CaseRecommended Client
Chat completions, tool callingOpenAI SDK or Anthropic SDK
Streaming data ingestionLayerScale SDK
Flash QueriesLayerScale SDK
Session managementLayerScale SDK
WebSocket connectionsLayerScale SDK
Quick prototypingOpenAI SDK (most familiar to developers)