πŸ˜ƒ Basics
🧠 Advanced
Zero-Shot
🟒 Introduction
🟒 Emotion Prompting
🟒 Role Prompting
🟒 Re-reading (RE2)
🟒 Rephrase and Respond (RaR)
🟦 SimToM
β—† System 2 Attention (S2A)
Few-Shot
🟒 Introduction
🟒 Self-Ask
🟒 Self Generated In-Context Learning (SG-ICL)
🟒 Chain-of-Dictionary (CoD)
🟒 Cue-CoT
🟦 Chain of Knowledge (CoK)
β—† K-Nearest Neighbor (KNN)
β—†β—† Vote-K
β—†β—† Prompt Mining
Thought Generation
🟒 Introduction
🟒 Chain of Draft (CoD)
🟦 Contrastive Chain-of-Thought
🟦 Automatic Chain of Thought (Auto-CoT)
🟦 Tabular Chain-of-Thought (Tab-CoT)
🟦 Memory-of-Thought (MoT)
🟦 Active Prompting
🟦 Analogical Prompting
🟦 Complexity-Based Prompting
🟦 Step-Back Prompting
🟦 Thread of Thought (ThoT)
Ensembling
🟒 Introduction
🟒 Universal Self-Consistency
🟦 Mixture of Reasoning Experts (MoRE)
🟦 Max Mutual Information (MMI) Method
🟦 Prompt Paraphrasing
🟦 DiVeRSe (Diverse Verifier on Reasoning Step)
🟦 Universal Self-Adaptive Prompting (USP)
🟦 Consistency-based Self-adaptive Prompting (COSP)
🟦 Multi-Chain Reasoning (MCR)
Self-Criticism
🟒 Introduction
🟒 Self-Calibration
🟒 Chain of Density (CoD)
🟒 Chain-of-Verification (CoVe)
🟦 Self-Refine
🟦 Cumulative Reasoning
🟦 Reversing Chain-of-Thought (RCoT)
β—† Self-Verification
Decomposition
🟒 Introduction
🟒 Chain-of-Logic
🟦 Decomposed Prompting
🟦 Plan-and-Solve Prompting
🟦 Program of Thoughts
🟦 Tree of Thoughts
🟦 Chain of Code (CoC)
🟦 Duty-Distinct Chain-of-Thought (DDCoT)
β—† Faithful Chain-of-Thought
β—† Recursion of Thought
β—† Skeleton-of-Thought
πŸ”“ Prompt Hacking
🟒 Defensive Measures
🟒 Introduction
🟒 Filtering
🟒 Instruction Defense
🟒 Post-Prompting
🟒 Random Sequence Enclosure
🟒 Sandwich Defense
🟒 XML Tagging
🟒 Separate LLM Evaluation
🟒 Other Approaches
🟒 Offensive Measures
🟒 Introduction
🟒 Simple Instruction Attack
🟒 Context Ignoring Attack
🟒 Compound Instruction Attack
🟒 Special Case Attack
🟒 Few-Shot Attack
🟒 Refusal Suppression
🟒 Context Switching Attack
🟒 Obfuscation/Token Smuggling
🟒 Task Deflection Attack
🟒 Payload Splitting
🟒 Defined Dictionary Attack
🟒 Indirect Injection
🟒 Recursive Injection
🟒 Code Injection
🟒 Virtualization
🟒 Pretending
🟒 Alignment Hacking
🟒 Authorized User
🟒 DAN (Do Anything Now)
🟒 Bad Chain
πŸ”¨ Tooling
Prompt Engineering IDEs
🟒 Introduction
GPT-3 Playground
Dust
Soaked
Everyprompt
Prompt IDE
PromptTools
PromptSource
PromptChainer
Prompts.ai
Snorkel 🚧
Human Loop
Spellbook 🚧
Kolla Prompt 🚧
Lang Chain
OpenPrompt
OpenAI DALLE IDE
Dream Studio
Patience
Promptmetheus
PromptSandbox.io
The Forge AI
AnySolve
Conclusion
🌱 New Techniques🟦 Code Prompting

Code Prompting

🟦 This article is rated medium
Reading Time: 3 minutes
Last updated on March 10, 2025

Valeriia Kuka

Takeaways
  • Code Prompting Overview: Code prompting enhances reasoning in large language models (LLMs) by transforming natural language tasks into structured code representations with conditional logic, which improves accuracy in conditional reasoning.
  • Performance Gains: This approach significantly boosts performance on conditional reasoning tasks across various models and datasets, proving to be more efficient with fewer training examples and improving variable tracking.
Overview of Code Prompting

What is Code Prompting?

Code prompting is a novel technique that enhances reasoning abilities in text+code Large Language Models (LLMs) by transforming natural language (NL) tasks into code representations. Instead of executing the code, the model uses it as a structured input format to reason and generate answers. This approach aims to improve conditional reasoning, where conclusions depend on specific conditions or logical steps like determining eligibility for a visa or loan based on given rules.

How Code Prompting Works

  1. Problem Conversion: Natural language problems are transformed into a code-like structure, with conditional logic and variables, while preserving the original text as comments.
  2. Prompting: The LLM receives the generated code, interprets the structure, and produces answers in natural language.

Example

A question like "Can a widow claim benefits?" is transformed into a code representation, with variables for key terms (e.g., "widow") and conditional logic for eligibility. The LLM uses this structured format to track variables and conditions effectively, improving its reasoning accuracy.

How to Use Code Prompting

Code prompting can be applied to any reasoning task requiring logical steps or condition-based conclusions. Below is a template for how the code transformation might look:

Astronaut

Template for Code Prompting


You are a helpful assistant. Your task is to process a pseudo-code that describes a question and a document. You need to reason using that document and the comments to return the answers. Answers must be a short span of the document. You have to extract the span from the code comments. Do not write anything else. I will give you some examples first.

Q1: [Question 1] A1: [Solution using preudo-code]

Q2: [Question 2] A2: [Solution using preudo-code]

Q3: [Your question] A3:

Answers must be a short span of the document. You have to extract the span from the code comments. Do not write anything else.

Let’s think step by step:

Tip

You can find original prompts and their implementations on GitGub.

Results of Code Prompting

The performance of code prompting was evaluated on three datasets that require conditional reasoning:

  • ConditionalQA (CondQA): Scenario-based reasoning
  • BoardgameQA (BGQA): Rule-based logic for board games
  • ShARC: Conversational question-answering
ModelCondQA (F1)ShARC (F1)BGQA-3 (F1)Avg. Gain (F1)
GPT-3.5+22.52%+8.42%+18.52%+8.53%
Mixtral+7.75%+4.22%+14.57%+4.23%
Mistral+16.78%+2.74%+5.93%+2.74%

Code prompts significantly outperformed text-based prompts across all models and datasets, showing the highest gains on reasoning-intensive tasks like BGQA.

  • Sample efficiency: Code prompts are more efficient, performing better with fewer training examples. For example, code prompts with only one demonstration outperformed text prompts with three demonstrations.
  • Improved variable tracking: Code formatting helps LLMs track variables and conditions more effectively, leading to fewer errors in reasoning, particularly for multi-step logical tasks.

Conclusion

Code prompting is an effective strategy for eliciting conditional reasoning abilities in text+code LLMs, enhancing their performance on logical tasks by providing structured inputs. This method not only improves accuracy but also reduces the need for extensive demonstrations, making it a valuable tool for improving reasoning in AI applications.

Valeriia Kuka

Valeriia Kuka, Head of Content at Learn Prompting, is passionate about making AI and ML accessible. Valeriia previously grew a 60K+ follower AI-focused social media account, earning reposts from Stanford NLP, Amazon Research, Hugging Face, and AI researchers. She has also worked with AI/ML newsletters and global communities with 100K+ members and authored clear and concise explainers and historical articles.