AI models & credits
Mini Programs can use Lexcetha's AI models to write, transcribe, speak, draw and make videos. You, the developer, pay for these calls in AI credits. The workspaces that install your Mini Program are never charged for them.
What's available
| Permission | SDK call | What it does |
|---|---|---|
ai.text | lex.ai.text() | Writes and answers text: one prompt or a conversation, up to 2048 tokens per answer |
ai.transcribe | lex.ai.transcribe() | Turns speech into text, up to 10 MB of audio per call |
ai.speak | lex.ai.speak() | Reads text aloud (MP3), up to 4096 characters per call |
ai.image | lex.ai.image() | Creates an image, at standard or high quality |
ai.video | lex.ai.video.* | Creates a short video. This takes minutes |
media.microphone | lex.media.recordAudio() | Records audio when the person allows it (free; no credits) |
Add the ones you use to manifest.permissions; reviewers approve them like any other permission. The full API is in the SDK reference.
Prices
| Capability | Credits |
|---|---|
ai.text | 1 per 1,000 tokens (prompt and answer together) |
ai.transcribe | 2 per MB of audio |
ai.speak | 1 per 1,000 characters |
ai.image | 4 per image (12 at high quality) |
ai.video | 40 per video |
Each call costs at least 1 credit. Credits are taken before the model runs and given back if the call fails, so a failed call costs you nothing. ai.text() returns what it cost in credits.
Daily limit per person
So no single person can use up your credits, each person has a daily credit limit in your Mini Program. Set it in your project's Store listing (default 100 credits a day; 0 turns AI off for everyone). When someone reaches it, ai.* calls fail with RATE_LIMITED until the next day. Handle it with a friendly message.
Previews
Previews in the contributor dashboard use the real models and are paid from your own credits. They have no daily limit, so watch what you spend while testing. Each preview call is marked as a preview in your credit history.
Getting credits
Credits are added to your account by Lexcetha. There is no self-serve purchase yet: contact Lexcetha to top up. The AI credits page in the contributor dashboard shows your balance, the prices, every top-up and charge, and what each Mini Program used in the last 30 days.
When your balance runs out, ai.* calls fail with INSUFFICIENT_CREDITS for everyone using your Mini Program, so keep an eye on it. If a model isn't set up on a Lexcetha instance, calls fail with UNAVAILABLE.
import { LexError } from '@lex/sdk';
try {
const { text } = await lex.ai.text({ prompt });
show(text);
} catch (e) {
if (!(e instanceof LexError)) throw e;
if (e.code === 'RATE_LIMITED') show('You reached today’s AI limit. Try again tomorrow.');
else if (e.code === 'INSUFFICIENT_CREDITS' || e.code === 'UNAVAILABLE') show('AI is not available right now.');
else throw e;
}Recording audio
Mini Programs run in a sandboxed iframe that can't use the microphone. With media.microphone, Lex records for you:
- Your Mini Program calls
lex.media.recordAudio(). - Lex checks that the permission was approved, then asks the person: "Your Mini Program wants to use your microphone".
- If they start, Lex shows its own recorder with a timer, Stop and Cancel. It stops on its own at
maxSeconds(default 600, at most 3600). - Your call resolves with the audio
Blob(usually WebM/Opus), or rejects withCANCELLEDif they cancel, orPERMISSION_DENIEDif the browser blocks the microphone.
Your code never sees the microphone itself, only the finished recording the person chose to share. Recording costs no credits; transcribing it does.
Example: AI meeting note taker
Needs media.microphone, ai.transcribe, ai.text and storage.scoped.
import { createLex, LexError } from '@lex/sdk';
const lex = createLex();
async function takeNotes() {
let audio: Blob;
try {
audio = await lex.media.recordAudio({ maxSeconds: 3600 });
} catch (e) {
if (e instanceof LexError && e.code === 'CANCELLED') return; // they changed their mind
throw e;
}
if (audio.size > 10 * 1024 * 1024) throw new Error('The recording is too long to transcribe in one go.');
status.textContent = 'Transcribing…';
const { text: transcript } = await lex.ai.transcribe(audio);
status.textContent = 'Summarizing…';
const { text: summary } = await lex.ai.text({
system: 'You turn meeting transcripts into short notes: a summary, decisions, and action items with owners.',
prompt: transcript,
maxTokens: 800,
});
await lex.storage.set(`note:${Date.now()}`, { transcript, summary });
render(summary);
}An hour of Opus audio is usually well under 10 MB; for longer meetings, record in parts and transcribe each one.
Example: motion graphic video
Needs ai.video.
button.onclick = async () => {
progress.hidden = false;
try {
const video = await lex.ai.video.create(`Motion graphic: the words "${title.value}" fly in over a soft gradient, clean and modern`, {
onProgress: (p) => (progress.value = p),
});
player.src = URL.createObjectURL(video);
} finally {
progress.hidden = true;
}
};A video usually takes a few minutes. Tell the person it's on its way, and keep the page open while it's made; to survive the Mini Program being closed, use ai.video.start() and store the jobId, then check ai.video.status() later.
