Examples Overview
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
Available Examples
Section titled “Available Examples”Lead Enrichment
Section titled “Lead Enrichment”Classify and prioritize sales leads based on notes and context. A classic B2B use case demonstrating nua.list() with complex schemas.
Sentiment Analysis
Section titled “Sentiment Analysis”Analyze customer feedback at scale. Shows batch processing with nua.list() and grouping results.
Data Extraction
Section titled “Data Extraction”Extract structured data from unstructured text. Demonstrates schema design and validation patterns.
Running the Examples
Section titled “Running the Examples”Install Nuabase and Zod:
npm install nuabase zodThe 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.
Schema Design Tips
Section titled “Schema Design Tips”When designing schemas for your use cases:
Keep Schemas Simple
Section titled “Keep Schemas Simple”// Good: focused schemaconst SentimentSchema = z.object({ sentiment: z.enum(['positive', 'neutral', 'negative']), confidence: z.number().min(0).max(1),});
// Avoid: overly complex schemasconst 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});Use Enums for Classification
Section titled “Use Enums for Classification”// Good: constrained optionsconst CategorySchema = z.enum(['bug', 'feature', 'question']);
// Less reliable: free-form stringconst CategorySchema = z.string();Make Optional Fields Explicit
Section titled “Make Optional Fields Explicit”const ContactSchema = z.object({ name: z.string(), email: z.string(), phone: z.string().optional(), // Explicitly optional company: z.string().optional(),});Add Validation Constraints
Section titled “Add Validation Constraints”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.
Keep the Prompt Constant
Section titled “Keep the Prompt Constant”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.
Prompt Writing Tips
Section titled “Prompt Writing Tips”Be Specific
Section titled “Be Specific”// Good: clear instructionsawait 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 interpretationawait nua.get("Classify this", options);Provide Context
Section titled “Provide Context”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 }});Match Prompt to Schema
Section titled “Match Prompt to Schema”const OutputSchema = z.object({ category: z.enum(['high', 'medium', 'low']), reason: z.string(),});
// Good: prompt matches schema fieldsawait nua.get("Rate the priority (high, medium, or low) and explain the reason", { output: { name: 'rating', schema: OutputSchema }});