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
npm install @layerscale/layerscalepip install layerscaleCreate 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}`);from layerscale import LayerScale
client = LayerScale("http://localhost:8080", api_key="LS-...")
session = 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}, ], mark_prefix=True,)
print(f"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 dataawait 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 });# Push OHLCV dataclient.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)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)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:
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 queryconst 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 queryfor 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);}# Blocking queryresult = client.sessions.query( session.session_id, prompt="What's the trend over the last hour?", max_tokens=200,)print(result.text)
# Streaming querywith client.sessions.query_stream( session.session_id, prompt="Describe the price action.", max_tokens=300,) as stream: for chunk in stream: if not chunk.done: print(chunk.token, end="", flush=True)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})`); }}with client.sessions.events(session.session_id) as events: for event in events: if event.type == "flash_ready": print(f"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 datasocket.push([{ o: 185.5, h: 186.2, l: 185.1, c: 185.8, v: 12500 }]);
// Listen for flash query resultssocket.on("flash_ready", (event) => { console.log(`${event.query}: ${event.value}`);});
socket.on("error", (err) => { console.error(`Error: ${err.message}`);});from layerscale import WsFlashReady
with client.sessions.stream(session.session_id) as socket: socket.push([{"o": 185.5, "h": 186.2, "l": 185.1, "c": 185.8, "v": 12500}])
for event in socket: if isinstance(event, WsFlashReady): print(f"{event.data.query}: {event.data.value}")For async code, use AsyncLayerScale and async with client.sessions.stream(...).
Stream Status and Stats
// Check streaming processor statusconst 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 dataconst stats = await client.sessions.stats(session.session_id);console.log(stats);# Check streaming processor statusstatus = client.sessions.stream_status(session.session_id)print(f"Queue: {status.streaming.queue_size}/{status.streaming.queue_capacity}")print(f"Processed: {status.statistics.items_processed} items")
# Get computed stats for the session's datastats = client.sessions.stats(session.session_id)print(stats)Delete a Session
await client.sessions.delete(session.session_id);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 + eventsconst 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);});import asynciofrom layerscale import AsyncLayerScale, WsFlashReady
async def main(): async with AsyncLayerScale("http://localhost:8080", api_key="LS-...") as client: 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}, ], mark_prefix=True, )
async with client.sessions.stream(session.session_id) as socket: async def push_loop(): while True: candle = get_latest_candle() # your feed await socket.push([{ "o": candle.open, "h": candle.high, "l": candle.low, "c": candle.close, "v": candle.volume, }]) await asyncio.sleep(1)
asyncio.create_task(push_loop())
async for event in socket: if isinstance(event, WsFlashReady): d = event.data print(f"[FLASH] {d.query}: {d.value} ({d.confidence})")
asyncio.run(main())When to Use Which Client
| Use Case | Recommended Client |
|---|---|
| Chat completions, tool calling | OpenAI SDK or Anthropic SDK |
| Streaming data ingestion | LayerScale SDK |
| Flash Queries | LayerScale SDK |
| Session management | LayerScale SDK |
| WebSocket connections | LayerScale SDK |
| Quick prototyping | OpenAI SDK (most familiar to developers) |