tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Prompt Engineering > Program of Thought Prompting - Code-Based Reasoning with LLMs

Program of Thought Prompting - Code-Based Reasoning with LLMs

Author: Venkata Sudhakar

Program of Thought (PoT) prompting asks an LLM to express its reasoning as executable code rather than natural language. At ShopMax India, calculating optimal discount tiers, margin analysis, or GST-inclusive pricing involves numerical reasoning where LLMs make arithmetic errors in prose. PoT sidesteps this by generating Python code that performs the calculation correctly, then executing it to get the verified answer.

The workflow has two steps: the LLM generates a Python code block that solves the problem (no prose reasoning, just code), and then the code is executed to produce the answer. This separates language understanding (where LLMs excel) from numerical computation (where Python excels). The LLM handles problem interpretation and algorithm design; Python handles the arithmetic. The result is far more reliable than asking the LLM to compute in its head.

The example below shows ShopMax India using PoT to calculate the optimal bulk discount structure for a corporate client order, and to compute the final GST-inclusive invoice total. Claude generates the code, Python runs it.


It gives the following output,

Generated code:
tv_price = 54999
tv_qty = 15
ac_price = 42500
ac_qty = 8

tv_subtotal = tv_price * tv_qty
ac_subtotal = ac_price * ac_qty
subtotal = tv_subtotal + ac_subtotal

if subtotal > 1000000:
    discount_rate = 0.08
elif subtotal > 500000:
    discount_rate = 0.05
else:
    discount_rate = 0.0

discount_amount = subtotal * discount_rate
discounted = subtotal - discount_amount

tv_gst = tv_subtotal * (1 - discount_rate) * 0.18
ac_gst = ac_subtotal * (1 - discount_rate) * 0.28
total_gst = tv_gst + ac_gst
final_total = discounted + total_gst

print(f"Subtotal: Rs {subtotal:,.0f}")
print(f"Discount rate: {discount_rate*100:.0f}%  Discount: Rs {discount_amount:,.0f}")
print(f"GST (TV 18% + AC 28%): Rs {total_gst:,.0f}")
print(f"Final total: Rs {final_total:,.0f}")

Execution result:
Subtotal: Rs 1,164,985
Discount rate: 8%  Discount: Rs 93,199
GST (TV 18% + AC 28%): Rs 185,831
Final total: Rs 1,257,617

At ShopMax India, always sandbox exec() calls - use RestrictedPython or a subprocess with limited permissions to prevent the generated code from accessing the file system or network. Validate the generated code with a regex check for dangerous patterns (import os, open(), requests) before executing. For recurring calculation types like invoice generation, cache the generated code templates and only re-generate when the business rules change, saving LLM API calls on every order.


 
  


  
bl  br