Welcome to the final chapter! ๐
In Chapter 4: MCP (Model Context Protocol) Integration, we learned how to pull in powerful tools from external servers. Now our menu is full of skills from local files, projects, plugins, and MCP servers.
But before you select a skill, there is one critical question: "How 'expensive' is this?"
In this chapter, we explore Token Estimation & Metadata.
Imagine going to a restaurant where the menu lists delicious food but no prices. You might accidentally order a dish that bankrupts you!
In the world of AI, Tokens are the currency.
We calculate the "weight" of a skill and display it right next to the name. It acts like a price tag or a file size indicator, helping you decide if you can "afford" to use that skill right now.
To an AI, a "token" is a chunk of text. Roughly speaking, 100 tokens $\approx$ 75 words.
Metadata is "data about data." In our Skills Menu, simply knowing the name of the skill isn't enough. We also display:
Here is what happens in the split second before the menu renders a skill line:
Let's look at how SkillsMenu.tsx calculates and displays this information.
We use a helper function called estimateSkillFrontmatterTokens. This function reads the text inside the skill and runs a quick math approximation.
// SkillsMenu.tsx
import { estimateSkillFrontmatterTokens } from '../../skills/loadSkillsDir.js';
import { formatTokens } from '../../utils/format.js';
// Inside the render function for a single skill
const estimatedTokens = estimateSkillFrontmatterTokens(skill);
// Format it nicely (e.g., turns 1500 into "1.5k")
const tokenDisplay = `~${formatTokens(estimatedTokens)}`;
Explanation:
We don't need an exact count (which is slow and depends on the specific AI model). We just need an estimate. The ~ symbol indicates this is an approximation.
If a skill comes from a plugin, the user needs to know which plugin. A generic skill named configure could belong to a "GitHub Plugin" or a "Jira Plugin."
// SkillsMenu.tsx
const pluginName =
skill.source === 'plugin'
? skill.pluginInfo?.pluginManifest.name
: undefined;
Explanation:
We check the source. If it is 'plugin', we dig into the pluginInfo object to find the human-readable name of the plugin manifest.
Finally, we combine the Name, the Plugin Origin, and the Token Cost into one visual line.
// SkillsMenu.tsx
return (
<Box key={skill.name}>
{/* 1. The Skill Name */}
<Text>{skill.name}</Text>
{/* 2. The Metadata (Dimmed Color) */}
<Text dimColor>
{pluginName ? ` ยท ${pluginName}` : ""}
ยท {tokenDisplay} tokens
</Text>
</Box>
);
Explanation:
<Text dimColor> so the metadata doesn't distract from the main skill name.pluginName exists, we add a bullet point (ยท) and the name. If not, we skip it.{tokenDisplay}.When you run the application, this code results in a clean list:
This empowers the user to manage their Context Window effectively.
Congratulations! ๐ You have completed the Skills project tutorial.
Let's recap what we've built:
You now understand the architecture behind a robust, user-friendly AI command menu!
Generated by Code IQ