How to Add a Codex Pet to Your Website as an Animated Companion
Codex has small animated pets that sit on your screen while it works. They wave and run around while you wait. The first time I saw one I started wondering why a website could not have the same thing, a tiny character that lives in the corner of a page and reacts to what you do.
It turns out a website can. This article shows how to put a Codex pet on a page with one script tag and how to do the same in React and Next.js. Along the way you will see how a pet can talk and how to react to your own buttons. I use Tanjiro from a community gallery in every example.
html<script type="module" src="https://unpkg.com/sprite-buddy@0.1.1/dist/browser.js"></script> <sprite-buddy pets="/pets/tanjiro/"></sprite-buddy>
Those two lines put a pet in the bottom right corner of an HTML page once the pet's folder sits at /pets/tanjiro/. Visitors can drag the pet around and click it. The next section explains where that folder comes from.
I built the first version of this for my own website and showed it to a few friends. They loved it and asked how they could add one to their own sites so I pulled the reusable parts out into an npm package called sprite-buddy. My own site still runs its original private version. The package is a separate rewrite for everyone else and it is an independent project that is not affiliated with OpenAI.
Everything in this article was run against sprite-buddy 0.1.1 in Chromium. The plain HTML parts ran on a small recipe blog that I made up for the screenshots and the React parts ran in a fresh Next.js 16 app with React 19.
What a Codex pet actually is
A Codex pet is a spritesheet. If you have ever worked on a 2D game you already know the idea because sprite sheets are one of the oldest tricks for animating characters. All the frames of a character are drawn onto one image and the program shows one frame at a time and swaps to the next on a timer.
In a Codex pet the frames sit in a grid and every row is one animation. One row is the character standing still and others are running left and running right and waving and jumping. The pet has nine of these rows which are called idle, runRight, runLeft, wave, jump, failed, waiting, running and review. Because it is only a still image that gets cropped and swapped a pet needs no video or GIF to move.
A pet is a folder with two files. One is the spritesheet image and the other is a small pet.json that names it. This is the one that came with Tanjiro.
json{ "id": "tanjiro", "displayName": "Tanjiro", "description": "A chibi Sun Breathing swordsman companion with calm focus and fiery sword animations.", "spritesheetPath": "spritesheet.webp" }
Sprite Buddy reads exactly that format which means a pet made for Codex runs on a website without being converted.
Get a pet from a gallery
Community galleries such as Codex Pets and Pets Codex share Codex pets. On Codex Pets each pet has a Download button and its dialog says to unzip the package into $HOME/.codex/pets/<pet-id> or to run npx codex-pets add <pet-id>. Either way you end up with the folder that holds pet.json and spritesheet.webp.
Sprite Buddy can check a folder before you use it.
shnpx sprite-buddy inspect ~/.codex/pets/tanjiro
For Tanjiro it printed Tanjiro (tanjiro): 1536x1872 webp, version 1. Copy that folder into your site so that it is served at /pets/tanjiro/ and you are ready for the next step.
These characters are fan art made by people in the community. The art stays the work of the people who made it and the characters belong to their rights holders. Credit the creator named on the gallery page and check that you may use a pet before you put it on a site. Sprite Buddy itself only ships two sample pets called Blip and Ember.
Add the pet to a plain HTML page
Paste the two lines from the start of this article before the closing body tag. The script registers a custom element called <sprite-buddy> and every <sprite-buddy> on the page turns into a pet. The pets attribute can point at a single pet folder as it does here or at a list of pets which the next section covers. I tried this on a made up site called Mira's Kitchen Notes. It has a home page with a featured recipe and a few cards below it so the pet sits on something that looks like a real page.
![]()
The element has two methods you will use most. say(text) shows a speech bubble and play(state) runs one of the nine animations. This is the code behind the Save recipe button.
html<button id="save">Save recipe</button> <script type="module" src="https://unpkg.com/sprite-buddy@0.1.1/dist/browser.js"></script> <sprite-buddy pets="/pets/tanjiro/"></sprite-buddy> <script type="module"> const buddy = document.querySelector('sprite-buddy'); document.getElementById('save').addEventListener('click', () => { buddy.say('Saved!'); buddy.play('jump'); }); buddy.addEventListener('buddy-click', () => { console.log('someone clicked the pet'); }); </script>
Clicking the button opened the bubble in the screenshot and clicking the pet fired buddy-click. That event is the hook for anything you want a click on the pet to do such as opening your own search or a help panel. The package also fires buddy-change when a visitor switches pets and buddy-error when a pet fails to load.
Only jump plays once. The other states loop until you play something else. If the pet does not appear the buddy-error event is the first place to look so listen for it and log event.detail.error.
Set it up properly on your own site
The script tag from the last section is the quickest way to try a pet. This section assumes you have a slightly bigger project and want more control. You want everything served from your own domain instead of unpkg and you want visitors to be able to switch between several pets. You may also want a settings page where they can see every pet and even drop in a spritesheet of their own. Sprite Buddy has a command line tool that sets all of that up for you.
1. Install the package and run init
Run these two commands in the root of your project, which is the folder that holds your index.html and package.json.
shnpm install sprite-buddy npx sprite-buddy init
init looks at your project and works out what kind it is. Plain HTML, Next.js, Astro and Vite are supported. Before it writes anything it prints a plan. This is what it printed for my plain HTML recipe site.
textProject: html added pets/blip/ (sample pet) created sprite-buddy/sprite-buddy.js created buddy-lab.html index.html needs these lines: + <sprite-buddy pets="pets/index.json" lab-url="buddy-lab.html"></sprite-buddy> + <script type="module" src="sprite-buddy/sprite-buddy.js"></script>
Here is what each line means.
pets/blip/is the sample pet. It gives you something to look at straight away and you can remove it later.sprite-buddy/sprite-buddy.jsis a copy of the library that lives in your own project. This is what replaces the unpkg script tag.buddy-lab.htmlis the settings page where visitors choose a pet or add their own.
The command asks before it edits your index.html and it never overwrites a file that already exists. If you only want to see the plan you can add --dry-run and nothing will be written.
2. Add the two lines to your page
init prints two lines that it needs in your index.html. The first is the element that shows the pet and the second loads the library from your own sprite-buddy/ folder.
html<sprite-buddy pets="/pets/index.json" lab-url="/buddy-lab.html"></sprite-buddy> <script type="module" src="/sprite-buddy/sprite-buddy.js"></script>
The pets attribute points at a list of pets and the lab-url attribute adds a link to your settings page in the pet's menu. I changed the paths to start with a slash because init writes them relative to the page. A page inside a subfolder such as /blog/ would otherwise look for pets/index.json in the wrong place and the pet would not load.
3. Add your pet
Now add Tanjiro to the list. This assumes you already downloaded his folder as described earlier in this article.
shnpx sprite-buddy add ~/.codex/pets/tanjiro npx sprite-buddy list
add copies the folder into pets/ and writes an entry for it in pets/index.json. list prints every pet that is now registered so you can confirm it worked. You can run add again for as many pets as you like. After adding Tanjiro my project looked like this.
textindex.html buddy-lab.html sprite-buddy/ sprite-buddy.js pets/ index.json blip/ tanjiro/ pet.json spritesheet.webp
The list file that add maintains is plain JSON.
json{ "pets": [ { "id": "blip", "name": "Blip", "path": "blip/", "lines": "blip/lines.json" }, { "id": "tanjiro", "name": "Tanjiro", "path": "tanjiro/" } ] }
4. Choose who starts on the page
With more than one pet in the list you decide who a new visitor sees first with the pet attribute.
html<sprite-buddy pets="/pets/index.json" pet="tanjiro" lab-url="/buddy-lab.html"></sprite-buddy>
Without it the first pet in the list starts. That is why I set it to Tanjiro. Once a visitor picks someone else their choice is remembered by their own browser and it wins over your default.
5. See what visitors get
The pet's menu lets a visitor switch to any pet in your list. The settings page goes a step further. It shows a large preview and a button for each of the nine animations and it lists every pet. It also has a box where a visitor can drop in a spritesheet of their own and the image is saved in that visitor's browser only.
![]()
6. Keep the files small
A downloaded spritesheet is heavy for a corner decoration because Tanjiro's file is about 2.2 MB. The CLI can shrink sheets as it adds them if the optional sharp package is installed.
shnpm install -D sharp
When I ran add in the Next.js app below with sharp installed it printed Added Tanjiro (tanjiro) to public/pets/tanjiro (711 KB) which is roughly a third of the original size.
7. Upgrading later
The copied sprite-buddy/sprite-buddy.js does not update itself and init will not replace a file that already exists. To upgrade you update the package and delete the copied file and then run init again so that it writes a fresh one.
Add the pet to React and Next.js
A plain script tag is fine for a static site but a React app deserves a component. Sprite Buddy ships React wrappers that are safe to render on the server and the init command knows how to set up Next.js, Astro and Vite as well as plain HTML. I tested the Next.js route in a new App Router app.
shnpm install sprite-buddy npx sprite-buddy init npx sprite-buddy add ~/.codex/pets/tanjiro
In a Next.js project init copied Blip into public/pets/ and created app/_buddy/Buddy.jsx and a settings page at app/buddy-lab/page.jsx. It also offered to edit app/layout.js and I accepted. I added the pet prop so that Tanjiro starts instead of Blip. This is the component.
jsx'use client'; import { SpriteBuddy } from 'sprite-buddy/react'; export default function Buddy() { return <SpriteBuddy pets="/pets/index.json" pet="tanjiro" labUrl="/buddy-lab" />; }
The 'use client' line is there because the wrapper attaches its event listeners in an effect. The README says the wrapper is safe to render on the server and the pet itself only comes alive once the page runs in the browser. The layout then renders the component once so the pet follows visitors from page to page.
jsximport Buddy from './_buddy/Buddy'; export default function RootLayout({ children }) { return ( <html lang="en"> <body> {children} <Buddy /> </body> </html> ); }
To make the pet react to a button I wrote a small client component. React state does not control the pet so I reached for the element and called the same methods as before.
jsx'use client'; export default function SaveButton() { return ( <button onClick={() => { const buddy = document.querySelector('sprite-buddy'); buddy.say('Saved!'); buddy.play('jump'); }} > Save recipe </button> ); }
I put the same recipe page into the Next.js app and clicking the button produced the bubble below. The /buddy-lab page rendered too.
![]()
The wrapper takes the attributes you have seen so far as props namely pets, pet, size, wander, labUrl, hideOn, inline, features, lines, sitemap and searchIndex. For events it has onPetClick, onPetChange and onError which are the React names for buddy-click, buddy-change and buddy-error. I took those from the package source and its tests and I did not run each of them by hand.
Other frameworks work through the plain element. Importing sprite-buddy/element defines <sprite-buddy> and <buddy-lab> and then you use them like any other tag. The README says this works in Vite, Astro, Vue and Svelte and that Vue needs to be told these are custom elements. I did not test Vue or Svelte for this article.
Make the pet talk on different pages
The pet only talks when it has lines. Each pet in pets/index.json can point at its own lines file and this is how Tanjiro got his.
json{ "pets": [ { "id": "tanjiro", "name": "Tanjiro", "path": "tanjiro/", "lines": "tanjiro/lines.json" } ] }
In the lines file each key is a path and * stands for anything.
json{ "/": ["Welcome in. Have a look around the kitchen.", "The lemon cake is the one to start with."], "/blog/*": ["Take your time with this one. I will wait right here.", "Read the method before you turn the oven on."], "*": ["I am glad you stopped by."], "idle": ["Still here if you need me."] }
On the recipe site the home page got a line from the / list and a recipe post at /blog/lemon-olive-oil-cake.html got one from the /blog/* list a couple of seconds after the page loaded. The most specific matching key wins and the pet never repeats the line it just said.
![]()
Visitors can rewrite lines in their own browser as well. That is a big enough topic that it gets its own article.
Settings you will reach for first
| Attribute | What it does |
|---|---|
pets | Where the pet is. It can be a single pet folder or an index.json listing several |
pet | The id of the pet to start with |
size | S, M or L which are 80, 104 and 136 pixels wide |
wander | Lets the pet stroll sideways along the bottom of the page now and then. It stays still for visitors who prefer reduced motion |
inline | Puts the pet in the page flow instead of floating in the corner |
hide-on | Paths where the pet stays away such as /checkout* |
lab-url | Adds a link to your settings page in the pet's menu |
features | Which parts to offer such as search, talk, pets and size |
The README lists every attribute and the sizes come from the package source.
Use one pet on several sites
If you run more than one site you do not have to copy the pet folder into each of them. You can host the folder once and point every site at it.
html<sprite-buddy pets="https://assets.example.com/pets/tanjiro/"></sprite-buddy>
The server that hosts the folder has to send an Access-Control-Allow-Origin header. Each site still needs the script tag or the package because that is what defines the element.
A visitor's choice of pet is stored by their browser for each site separately. That is how browser storage works in general and I did not look for a way around it so someone who changes the pet on one of your sites will see the default again on the next.
When nothing shows up
The pet never renders on the server. If you use a framework that renders HTML ahead of time you need the React wrapper or an import in client code as shown above.
A pet hosted on a different domain needs the CORS header from the previous section. Sites with a strict content security policy need img-src blob: so that pets visitors upload can display. That second point comes from the package README and I did not test it here.
The pet also opens a page search with Ctrl+K or Cmd+K. If your site already uses that shortcut you can leave search out of the features attribute with something like features="talk pets size" and the shortcut then goes away. I took this from the README as well.
Where to go next
The package is on npm and the source and full README are on GitHub. The README is the reference for every option and I will keep these articles pinned to the versions I tested. A later article covers per page and per pet dialogue in detail.
Related Posts
Continue exploring similar topics

How I Built Neon Orbit, a 3D Game Universe in Three.js
How I turned my Playground's game cards into a 3D solar system with React Three Fiber and why AI tools couldn't make me a 3D character.
Spring Boot 3.x: What Actually Changed (and What Matters)
A practical look at Spring Boot 3.x features that change how you build services - virtual threads, reactive patterns, security gotchas, and performance lessons from production.

Conventional Commits Specification
The Conventional Commits specification is a lightweight convention that provides a standardized format for writing commit messages.