npx skills add ...
npx skills add picsart/gen-ai-skills --skill dev-avatar-service
Deterministic default-avatar generator per user.
npx skills add picsart/gen-ai-skills --skill dev-avatar-service
A service endpoint that returns a branded avatar for any user: photoreal, illustrated, or pixel, deterministic from a seed (user ID / email hash / initials). Built around a fallback hierarchy — real photo → upload → AI from initials → static default — with caching by seed so you pay once per user.
Not for: deepfaking real people from a single photo (wrong tool + wrong ethics), or stylizing an already-great uploaded photo — let users keep their own.
Before building, ask (one message, 5 questions):
Skip if the user hands over a detailed brief.
Decide the fallback hierarchy first. This is the whole service:
Estimate + validate model.
Deterministic seed → prompt. Hash the seed into stable attribute picks. Never inject Math.random() or timestamps.
Cache the URL (not the binary) keyed on the seed. Any store: Redis, KV, Postgres column, S3 + stable filename.
Initials fallback (zero credits). When gen-ai is down, rate-limited, or over budget — return an SVG built from the user's initials on a hue derived from the same seed. Users never see a broken image.
Wire the endpoint. /avatar/:seed → check photo → check cache → gen-ai → initials. 302 to CDN URL on success; inline SVG on fallback.
Pre-warm the cache in batch for existing users so your prod rollout doesn't hammer gen-ai live:
Backfill 500 existing users:
Output lands at ./avatars-out/<user-id>.webp — upload to your CDN under the same key. results.json becomes the seed → URL lookup table.
| Sub-task | Model | Why |
|---|---|---|
| Illustrated / vector / flat avatars | recraftv4 | Clean, consistent, cheap — ideal default |
| Photoreal portrait (synthetic, generic person) | flux-2-pro | Best faces; use with generic descriptors only |
| Pixel-art avatars | recraftv4 with pixel-style prompt | Lowest credits, stylistic control |
| Stylize an uploaded real photo (opt-in) | flux-kontext-pro + -i photo.jpg | Edit model preserves identity |
| Cheap draft / style exploration | gemini-3.1-flash-image | Lowest per-call cost |
| Identity-locked variants across sizes | ideogram-character + -i master.png | Same face across multiple avatars |
Verify live IDs with gen-ai models --mode image.
V=2 in the cache key when you restyle; old avatars age out, new ones regenerate on demand.text, watermark, letters, realistic face of specific person — avatars must not resemble real celebrities or render accidental copy.--save-to-drive --drive-folder avatars gives a stable CDN URL with no extra infra./avatar/:random can burn your credit budget in minutes.| Pitfall | Fix |
|---|---|
| Regenerated avatar looks different on every refresh | Non-deterministic prompt — hash the seed, derive attributes, no RNG/timestamps |
| Cache never invalidates after restyle | Version the prompt template (V=3 in the cache key) |
| AI renders real-looking faces resembling celebrities | Add negativePrompt: "realistic face of specific person, celebrity, known individual" |
| Text or watermarks appear in the avatar | Add negativePrompt: "text, watermark, letters, logos" |
| Bot traffic burns credit budget | Rate-limit per IP; allow only authed users to hit the generator; fallback to initials for anons |
| Gen-ai 429s cause broken avatar UI | SVG initials fallback must be the last step in the chain |
| Batch backfill half-fails | gen-ai batch resume <out> retries only failures |
| Different avatar style across the app | Commit the prompt template; never let callers pass raw prompts |
Run gen-ai whoami to confirm authentication, then re-run the failed command with --debug.
| Task | Credits | Time |
|---|---|---|
| 1 avatar (Recraft V4, illustrated) | 1-2 | ~4-8s |
| 1 avatar (Flux 2 Pro, photoreal) | 3-5 | ~10-15s |
| 1 avatar (Gemini 3.1 Flash, draft) | 1 | ~3-5s |
| Initials SVG fallback | 0 | <1ms |
| Cache read (KV/Redis hit) | 0 | ~2-5ms |
| Batch of 500 at concurrency 4 | ~750-1000 | ~15-20 min |
In steady state, expect >99% cache hit rate — near-zero ongoing cost.
gen-ai-use.md — CLI reference, auth, scripting with --json --no-input / jqgen-ai-batch.md — manifest shapes, batch resume, rate-limit strategy, Cloudflare Worker patterngen-ai-workflows.md — Workflow 6 (headshot studio) for stylizing uploaded photosdev-og-image-service — same caching + determinism patterns for OG cards