#15054 · @alberti42 · opened Feb 25, 2026 at 11:58 AM UTC · last updated Mar 21, 2026 at 8:59 PM UTC
fix(opencode): instruct all chat agents to use KaTeX-compatible math syntax
Score breakdown
Impact
Clarity
Urgency
Ease Of Review
Guidelines
Readiness
Size
Trust
Traction
Summary
This PR fixes a critical bug where LLM-generated math equations were unreadable due to incorrect LaTeX syntax for KaTeX rendering. It achieves this by injecting explicit math formatting rules into agent system prompts. The solution is clearly explained and visually verified with before/after screenshots.
Description
Issue for this PR
Closes #15053
Type of change
- [x] Bug fix
- [ ] New feature
- [ ] Refactor / code improvement
- [ ] Documentation
What does this PR do?
The opencode frontend renders math with KaTeX, which requires $...$ for
inline equations and $$...$$ (flush-left, surrounded by blank lines) for
display equations. Without explicit instructions, LLMs default to \(...\)
and \[...\] or wrap display equations in fenced code blocks — none of which
KaTeX can render. The result is raw LaTeX source appearing in the chat instead
of rendered formulas.
This PR adds a shared math_formatting_rules.txt file and injects it into
every primary-agent system prompt via a {MATH_FORMATTING_RULES} placeholder.
The placeholder is inserted inside the existing formatting/style section of
each prompt rather than appended at the end. This matters: LLMs follow
instructions better when related rules are grouped together. Appending at the
end (e.g. via AGENTS.md) would scatter formatting guidance across the
prompt, with general style rules at the top and math rules at the bottom.
Placing them together gives the agent a coherent picture of all formatting
expectations in one place.
beast.txt (used for sub-agents that never produce chat output) is excluded
intentionally. The token overhead is negligible (~13 lines).
One implementation note worth calling out: the injection uses
prompt.replace("{MATH_FORMATTING_RULES}", () => rules) with a function
rather than a plain string. JavaScript's replace() treats $ specially in
replacement strings (e.g. $` inserts the portion before the match), so
a plain string would silently corrupt the LaTeX delimiters. Passing a function
bypasses this entirely.
How did you verify your code works?
Tested manually with OpenAI (GPT-4o / GPT-5), Claude (claude-sonnet), and Gemini (gemini-2.5-pro) by asking each agent to write a response containing both inline and display equations. All three correctly produced KaTeX-renderable output after the fix.
Screenshots / recordings
Before — raw LaTeX source dumped as plain text, unreadable:
After — equations rendered by KaTeX, properly formatted:
Checklist
- [x] I have tested my changes locally
- [x] I have not included unrelated changes in this PR
Linked Issues
#15053 bug: math equations rendered as raw LaTeX source — agents not instructed to use KaTeX-compatible syntax
View issueComments
PR comments
alberti42
@adamdotdevin @Hona - gentle ping. I would appreciate feedback when you have a moment. I am glad to invest more time, add more tests for other providers, and polish it along your indications.
Changed Files
packages/opencode/src/session/prompt/anthropic.txt
+1−0packages/opencode/src/session/prompt/codex_header.txt
+1−0packages/opencode/src/session/prompt/gemini.txt
+1−0packages/opencode/src/session/prompt/math_formatting_rules.txt
+12−0packages/opencode/src/session/prompt/qwen.txt
+1−0packages/opencode/src/session/prompt/trinity.txt
+1−0packages/opencode/src/session/system.ts
+12−6