Skip to content

The SymPy skill: exact symbolic math for AI agents

An introduction to the SymPy skill for exact symbolic mathematics in AI agents: what it does, when to use it, and how to check the results.

Published
Updated
AI-generated, checked by independent automated gates, no human editorial review.
How this guide was made
The SymPy skill: exact symbolic math for AI agents

AI-generated. Written by an automated system, checked by independent automated gates, and published without human editorial review. How we label and verify.

Most numbers an AI agent touches are approximations. Ask for the square root of two and you usually get 1.4142… — close enough, until it isn’t. The SymPy skill exists for the moments when “close enough” is the bug, not the answer: it computes with mathematical symbols, so you get exact results like sqrt(2), 1/3, or a closed-form derivative instead of a rounded decimal.

SymPy is a mature Python library for symbolic mathematics — this skill is packaged by K-Dense Inc. on top of the open-source SymPy project. This spotlight covers what the skill lets an agent do, when it earns its place in a workflow, and the practical habits that keep it from tripping.

What the SymPy skill covers

It spans the working surface of computer algebra:

  • Algebra and simplification — expand, factor, cancel and simplify expressions (simplify(sin(x)**2 + cos(x)**2) returns 1).
  • Calculus — derivatives, indefinite and definite integrals, limits and series expansions.
  • Equation solving — algebraic equations, linear and nonlinear systems, and differential equations via solveset, linsolve, nonlinsolve and dsolve.
  • Matrices and linear algebra — inverses, determinants, eigenvalues and eigenvectors, diagonalization, and solving Ax = b symbolically.
  • Physics — classical mechanics (including Lagrange’s method), vector analysis, and quantum-mechanics primitives like kets, bras and commutators.
  • Advanced topics — geometry, number theory, combinatorics, logic and set theory, statistics, special functions, and polynomials up to Gröbner bases.
  • Code generation and output — turn an expression into a fast NumPy function, emit C or Fortran, or render LaTeX for documents.

A few things it does in one line

solve(x**2 - 5*x + 6, x)             # [2, 3]
diff(sin(x**2), x)                   # 2*x*cos(x**2)
integrate(x*exp(-x**2), (x, 0, oo))  # 1/2
Matrix([[1, 2], [2, 1]]).eigenvals() # {3: 1, -1: 1}
latex(Integral(x**2, (x, 0, 1)))     # ready-to-paste LaTeX

When to reach for it

Symbolic math is the right tool when exactness matters more than raw speed: deriving a formula, verifying an algebraic step, producing a closed-form result, or generating LaTeX and runnable code from the same expression. For heavy numerical crunching, the skill’s own advice is to convert the symbolic result into a compiled NumPy function with lambdify and let NumPy do the loops — symbolic to set up, numeric to run.

Habits that keep it honest

  • Define symbols first. Every variable comes from symbols('x y z') before it appears in an expression.
  • Use assumptions. Declaring x = symbols('x', positive=True) lets sqrt(x**2) simplify to x instead of Abs(x).
  • Stay exact. Write Rational(1, 2) or S(1)/2, not 0.5 — a stray float quietly turns exact math into approximate math.
  • Lambdify for loops. Repeated subs() plus evalf() is slow; one lambdify call is the fast path.
  • Match the solver to the problem — solveset for algebra, linsolve/nonlinsolve for systems, dsolve for differential equations.

Where the SymPy skill fits

In the AOF skill library, SymPy is the “give me the exact answer, plus the code and the LaTeX to go with it” tool. It pairs naturally with numerical and plotting skills such as NumPy, Matplotlib and SciPy: reason symbolically, then hand the compiled function to the numerical stack. When an agent needs to be right to the last term rather than the third decimal, this is the one to load.

Built on the open-source SymPy project; skill packaged by K-Dense Inc. The full library of 1,048 skills is available to members.

What the documentation and research say about exact math

The case for symbolic computation starts with a plain fact about ordinary computer numbers. Python’s own tutorial explains that the decimal value 0.1 cannot be represented exactly as a base 2 fraction. The machine stores the closest binary value it can, and the error is hidden by the way the number is printed. The same page shows the classic surprise: 0.1 + 0.1 + 0.1 == 0.3 evaluates to False. It also stresses that this is not a bug in Python. It is how binary floating point works, and each further operation can suffer a new rounding error.

For most work that is fine. For a derivation, a proof step or a formula that will be reused a thousand times, small errors compound or hide a result that should have been exactly zero.

Where floats sneak into symbolic code

The library’s documentation names the most common leak directly. If you write a plain number divided by a number inside an expression, Python will evaluate the two numbers before SymPy has a chance to get to them. So x + 1/2 arrives as x + 0.5, and the exact half is already gone. The fix is to wrap one side, as in Rational(1, 2) or S(1)/2.

Going the other way is deliberate and controlled. The numerical evaluation guide says exact expressions can be converted to floating-point approximations with .evalf() or N(), and that numerical evaluation is performed to an accuracy of 15 decimal digits by default, with more available on request. The point is that you choose when to approximate, and how precisely, instead of letting it happen silently on the first line.

That matters for agents: SymPy runs as an ordinary Python library, with no separate math engine to install or license.

Why language models need a calculator at all

Research on large language models points the same way. The authors of PAL (Program-aided Language Models) observed that models are good at breaking a word problem into steps, yet often make logical and arithmetic mistakes in the solution part even when the decomposition is right. Their method has the model write a program and offloads the solution step to a runtime such as a Python interpreter. On the GSM8K math benchmark that approach beat a much larger chain-of-thought model by absolute 15% top-1.

The Toolformer paper makes a related point from the other side. Large models, its authors note, struggle with basic functionality, such as arithmetic, where far smaller systems excel. Their model learned to call external tools, including a calculator, and decide for itself when a call was worth making.

A third line of work shows how fragile unaided math can be. In the GSM-Symbolic study, model performance declines when only the numerical values in the question are altered. Same problem, different numbers, worse answers. A computer algebra system does not care which numbers you pick.

How this lines up with the habits above

Each habit in the list earlier in this spotlight maps onto one of these documented failure points.

  • “Stay exact” is the gotchas page in practice. Writing Rational(1, 2) instead of 0.5 keeps Python’s float arithmetic out of the expression before it can round anything.
  • “Lambdify for loops” is the controlled version of approximation. You keep the exact form while deriving, then hand a compiled numeric function to NumPy when speed matters. Approximation becomes a decision, not an accident.
  • The examples earlier, such as integrate(x*exp(-x**2), (x, 0, oo)) returning 1/2, are exactly the kind of result a float pipeline would return as 0.49999999999 or similar. The exact form can be checked by eye.
  • Using the skill at all is the PAL idea in miniature: the agent sets up the problem, and a deterministic engine does the algebra.

The research does not say tools fix everything. PAL still depends on the model writing a correct program, and a symbolic engine will faithfully solve the wrong equation if that is what it receives. Defining symbols first and declaring assumptions are guards against that second failure: they make the agent state what each variable is before any math happens.

A checklist for exact math in agent workflows

  1. Scan for bare division. Any 1/3 or 2/5 between plain integers in an expression is a float. Replace it with Rational or S().
  2. Avoid float literals for known values. If a coefficient is exactly one half, write it as a fraction. Keep decimals for measured data only.
  3. Use symbolic constants. Prefer the library’s own pi and E over constants from Python’s math module, which gives math.pi only to available precision.
  4. Declare assumptions up front (positive, real, integer) so simplification can go as far as the math allows.
  5. Approximate once, at the end. Call evalf() or N() with the precision you need, or lambdify for repeated evaluation.
  6. Check a result by substitution. Plug a solution back into the original equation and simplify. An exact zero is a proof; a tiny decimal is a warning.
  7. Let the model plan and the engine compute. Do not ask the language model to do the arithmetic it just set up.

More questions

Why does adding 0.1 three times not give 0.3 in Python?

Because 0.1 is not stored exactly. Python’s tutorial explains that such values cannot be represented exactly as a base 2 fraction, so each copy is a close binary approximation and the sum carries the error. An exact rational avoids this because the fraction stays a fraction until you ask for a decimal.

If language models can write code, why not let them compute the answer directly?

Because writing the steps and executing them are different skills. PAL’s authors found models often make logical and arithmetic mistakes in the execution step, and GSM-Symbolic found accuracy drops when only the numbers change. Handing the calculation to an interpreter or a computer algebra system removes the part models are worst at.


Related guides

Agent Skills: The Four Bytes Four Gates Missed
Skill Spotlights · Published

Agent Skills: The Four Bytes Four Gates Missed

Four inspection gates checked 22 agent skills but missed four incorrect bytes. This case shows why teams need different verification methods, not simply more checks of the same kind.

What the JetEngine REST API actually reaches
Skill Spotlights · Published

What the JetEngine REST API actually reaches

JetEngine's REST API exposes two routes with different authentication behavior. Learn what the live endpoint tests proved, and where reads and writes stop.

The Slide Deck Check That Read Four-Pixel Text
Skill Spotlights · Published

The Slide Deck Check That Read Four-Pixel Text

A slide deck passed its checks despite four-pixel text. Learn to measure rendered legibility at delivery resolution and verify citations against their exact source spans.