Skip to content

Examples Overview

View as Markdown

This section contains complete, real-world examples showing Nuabase in action. Each example includes:

  • A problem description
  • Complete, copy-paste ready code
  • Expected output
  • Variations

Classify and prioritize sales leads based on notes and context. A classic B2B use case demonstrating nua.list() with complex schemas.

View Example

Analyze customer feedback at scale. Shows batch processing with nua.list() and grouping results.

View Example

Extract structured data from unstructured text. Demonstrates schema design and validation patterns.

View Example

Install Nuabase and Zod:

Terminal window
npm install nuabase zod

The examples use whichever coding agent is installed and logged in on your machine:

import { Nua, z } from 'nuabase';
import { findLocalAgent } from 'nuabase/local-agent';
const nua = Nua.direct({ localAgent: await findLocalAgent() });

To run them against an LLM API instead, swap the client:

import { Nua, z } from 'nuabase';
const nua = Nua.direct({
providers: { openrouter: { apiKey: process.env.OPENROUTER_API_KEY! } },
});

The rest of each example is the same with either client.

When designing schemas for your use cases:

// Good: focused schema
const SentimentSchema = z.object({
sentiment: z.enum(['positive', 'neutral', 'negative']),
confidence: z.number().min(0).max(1),
});
// Avoid: overly complex schemas
const OverlyComplexSchema = z.object({
sentiment: z.object({
primary: z.enum(['positive', 'neutral', 'negative']),
secondary: z.array(z.string()),
scores: z.object({
positive: z.number(),
neutral: z.number(),
negative: z.number(),
}),
}),
// ... many more nested fields
});
// Good: constrained options
const CategorySchema = z.enum(['bug', 'feature', 'question']);
// Less reliable: free-form string
const CategorySchema = z.string();
const ContactSchema = z.object({
name: z.string(),
email: z.string(),
phone: z.string().optional(), // Explicitly optional
company: z.string().optional(),
});

Every answer is parsed with your schema, and an answer that breaks a constraint is retried:

const MetricsSchema = z.object({
score: z.number().min(0).max(100),
count: z.number().int().nonnegative(),
percentage: z.number().min(0).max(1),
zip: z.string().regex(/^\d{5}$/),
});

.refine() checks, string formats such as z.email(), .default()s, and .transform()s all apply to result.data. See How It Works.

Put everything that varies in input, not in the prompt string. Providers cache the unchanging start of each request, which makes repeated calls cheaper and faster. See prompt caching.

// Good: clear instructions
await nua.get("Classify the customer support ticket into one of the following categories: bug report, feature request, billing question, or general inquiry", options);
// Vague: leaves too much interpretation
await nua.get("Classify this", options);
await nua.get("You are analyzing sales leads for a B2B SaaS company. Classify each lead by their likely company size and purchase intent.", {
input: leadNotes,
output: { name: 'classification', schema: LeadSchema }
});
const OutputSchema = z.object({
category: z.enum(['high', 'medium', 'low']),
reason: z.string(),
});
// Good: prompt matches schema fields
await nua.get("Rate the priority (high, medium, or low) and explain the reason", {
output: { name: 'rating', schema: OutputSchema }
});