How to configure AI for the Gram CLI
The Gram CLI (@gram-lang/cli) can leverage Large Language Models (LLMs) to automate tedious tasks such as converting web recipes into .gram files, detecting ingredient duplicates in your database, and filling missing nutritional and physical metadata.
This guide explains how to set up and configure your preferred AI provider step by step.
What AI powers in the Gram CLI
Section titled “What AI powers in the Gram CLI”Configuring an AI provider unlocks three powerful CLI features:
- Recipe Import (
gram import <source>): Automatically converts web pages (extracting JSON-LD recipe metadata) or local JSON-LD recipe files into valid.gramsyntax in your target language. - Database Deduplication & Linting (
gram db lint): Uses AI to detect semantic duplicates, singular/plural mismatches, and multi-language entries iningredients.yaml, interactively merging them into primary keys and aliases. - Database Enrichment (
gram db enrich): Queries nutritional values (calories, macros, micronutrients) and physical properties (densities, yields, unit weights) to propose fills for incomplete entries iningredients.yaml— density/nutrition proposals go through an interactive review before being written, since they’re product-specific (see Where the Numbers Come From for why Gram doesn’t ship a reference database instead).
Why AI? Restructuring vs. transliteration
Section titled “Why AI? Restructuring vs. transliteration”Gram is not just a markup format; it is a domain-specific model for recipes. Converting a traditional recipe into .gram syntax is not a mechanical string transformation.
Traditional recipes contain many steps that exist only because unstructured text has no other way to express them:
- “Cut 1 lemon in half” → In Gram, this dissolves into an inline preparation annotation on the ingredient:
@lemon{1}(cut in half). - “In a small bowl, combine salt and pepper and set aside” → In Gram, this becomes an intermediate variable reference (
&seasoning). - “Remove from heat and let cool for 30 minutes” → In Gram, this is a passive rest timer annotation (
~_{30min}).
A mechanical scraper or simple JSON parser cannot perform this culinary restructuring. It would generate verbose, imperative steps that miss Gram’s core features (mass standardization, dynamic scaling, ingredient reuse with @&, and composite ingredients like @lemon juice{}<@lemon{1}).
AI acts as a domain-aware recipe compiler. It analyzes unstructured recipe text, extracts the underlying culinary logic, absorbs preparation steps directly into ingredient annotations, restructures the workflow into logical sections, and outputs clean, idiomatic .gram code that a human expert recipe author would write.
Similarly, for database management:
gram db lint: AI detects semantic equivalence across phrasings and languages (e.g. knowing thatsoftened butter,butter,unsalted butter, andbeurrerefer to the same canonical ingredient).gram db enrich: AI proposes standardized physical densities (volume-to-mass conversion) and nutritional facts from culinary domain knowledge, without requiring manual lookup — but you still review and approve each proposal before it’s written.
Configuration methods
Section titled “Configuration methods”You can configure AI using interactive setup, environment variables (recommended for security), or configuration files.
Method 1: Interactive setup with gram init
Section titled “Method 1: Interactive setup with gram init”If you are initializing a new project or want a guided setup, run:
During initialization, the CLI will ask:
- Configure AI provider now?: Select Yes.
- Select an AI provider: Choose between Google (Gemini), OpenAI (ChatGPT), Anthropic (Claude), or Ollama (Local).
- Select a model: Choose a recommended model or specify a custom one.
- Save API key in
.env?: Confirm to save your key securely. The CLI will automatically create/update your.envfile and append.envto your project’s.gitignorefile to prevent accidental credential leaks.
Method 2: Environment variables (recommended)
Section titled “Method 2: Environment variables (recommended)”The Gram CLI automatically checks environment variables for credentials. On both Node.js (>=20.6.0) and Bun, .env files in your project root are automatically loaded.
1. Provider environment variable names
Section titled “1. Provider environment variable names”Export the appropriate environment variable for your provider:
| Provider | Environment Variable |
|---|---|
| Google Gemini | GEMINI_API_KEY |
| OpenAI | OPENAI_API_KEY |
| Anthropic | ANTHROPIC_API_KEY |
| Ollama (Local) | OLLAMA_BASE_URL (optional) |
2. Create a .env file
Section titled “2. Create a .env file”Add your key to a .env file at the root of your project:
Method 3: Configuration files (config.yaml)
Section titled “Method 3: Configuration files (config.yaml)”You can explicitly set your AI provider, model, or custom endpoint in your project’s .gram/config.yaml or globally in ~/.config/gram/config.yaml.
Global vs. project configuration
Section titled “Global vs. project configuration”- Global Config (
~/.config/gram/config.yaml): Applies system-wide for all Gram projects on your machine. - Project Config (
.gram/config.yaml): Overrides global settings for the current project.
Method 4: Running local AI with Ollama
Section titled “Method 4: Running local AI with Ollama”If you prefer running AI models locally without sending data to external APIs, you can use Ollama:
-
Install and run Ollama locally:
-
Configure Gram to use Ollama: Add the following to
.gram/config.yaml:
Dynamic overrides via flags
Section titled “Dynamic overrides via flags”You can override your default provider or model on the fly for any single command without editing your configuration:
See Choosing the AI model for full details.
Verifying your setup
Section titled “Verifying your setup”To test if your AI setup is working properly:
-
Test Ingredient Enrichment (Report Mode):
If AI is correctly configured, the command will analyze incomplete ingredients and preview what needs review, without error and without prompting or writing anything.
-
Import a Web Recipe:
Troubleshooting
Section titled “Troubleshooting”Missing API key error
Section titled “Missing API key error”If you see the following error:
- Ensure your
.envfile is in the project root directory where you rungram. - If using environment variables directly in terminal, export the variable beforehand (
export GEMINI_API_KEY=...). - Verify that your API key matches the provider configured in
.gram/config.yaml.