# How the ngpt Code Generation Mode Ensures Clean, Markdown-Free Output

> Discover how ngpt code generation mode delivers clean, markdown-free output using CLI flags, system prompts, and API parameters. Ensure your code is always pristine.

- Repository: [nazDridoy/ngpt](https://github.com/nazdridoy/ngpt)
- Tags: internals
- Published: 2026-03-07

---

**The ngpt code generation mode guarantees markdown-free output by coordinating a `--plaintext` CLI flag, specialized system prompts, and a `markdown_format` API parameter that explicitly instructs the LLM to omit code fences.**

The `nazdridoy/ngpt` repository provides a dedicated code generation mode that can return either formatted Markdown blocks or raw plain-text code. This functionality centers on [`ngpt/cli/modes/code.py`](https://github.com/nazdridoy/ngpt/blob/main/ngpt/cli/modes/code.py), where the application orchestrates three distinct mechanisms to strip Markdown formatting when requested. Understanding how these components interact ensures you receive clean, copy-paste-ready code without manual cleanup.

## The Three-Layer Mechanism for Markdown-Free Code Generation

The code generation mode implements a coordinated strategy across the CLI interface, prompt engineering, and API communication layers.

### The --plaintext Flag

When you append the `--plaintext` flag to the `ngpt code` command, the CLI sets `args.plaintext` to `True`. This boolean acts as the primary switch that cascades through the entire execution pipeline. In [`ngpt/cli/modes/code.py`](https://github.com/nazdridoy/ngpt/blob/main/ngpt/cli/modes/code.py) (lines 20-30), the argument parser defines this flag, which subsequently determines both the system prompt selection and the API call parameters.

### System Prompt Templates

The mode maintains two distinct system prompt templates that explicitly instruct the LLM on output formatting:

- **`CODE_SYSTEM_PROMPT_MARKDOWN`**: Requests code wrapped in Markdown code fences (e.g., ```python)
- **`CODE_SYSTEM_PROMPT_PLAINTEXT`**: Explicitly demands "only plain text without Markdown formatting"

When `args.plaintext` is `True`, the code constructs the `system_prompt` variable using the plaintext template (lines 73-81 in `code.py`). This prompt engineering ensures the model receives unambiguous instructions to omit Markdown syntax before generation begins.

### The markdown_format API Parameter

The `NGPTClient.chat()` method in `ngpt/api/client.py` accepts a `markdown_format` boolean parameter that controls backend behavior. In the code generation mode's execution flow (lines 45-53 of `code.py`), the call passes `markdown_format=not args.plaintext`. 

When the `--plaintext` flag is active, this evaluates to `False`, explicitly instructing the underlying OpenAI-compatible service to suppress Markdown code fences in the response. This API-level enforcement acts as the final safeguard against unwanted formatting.

## Step-by-Step Execution Flow in code.py

The implementation follows a precise sequence to ensure consistent output formatting:

1. **Input Gathering** (lines 91-110): The function collects user input from stdin, the `--prompt` argument, or an interactive editor session.
2. **Mode Selection** (lines 73-78): If `--plaintext` is supplied, the code sets the plaintext system prompt and prepares for raw output.
3. **Prompt Construction** (lines 72-82): The system prompt is formatted using either `CODE_SYSTEM_PROMPT_PLAINTEXT` or `CODE_SYSTEM_PROMPT_MARKDOWN` based on the flag state.
4. **API Invocation** (lines 45-53): The `client.chat()` method receives the constructed messages and the `markdown_format=not args.plaintext` parameter.
5. **Output Rendering** (lines 77-80): For plaintext mode, the response prints directly without passing through `prettify_streaming_markdown()`. In Markdown mode, the renderer adds syntax highlighting and code fences.

## Practical Examples: Markdown vs. Plain Text Output

The difference between default and plaintext modes is immediately visible in terminal output.

### Default Markdown Output

When running without flags, the code generation mode returns formatted Markdown:

```bash
ngpt code --language python "sort a list of integers"

```

The terminal displays a syntax-highlighted block:

```python
def sort_numbers(nums):
    return sorted(nums)

```

### Plain-Text Mode Output

Adding the `--plaintext` flag removes all formatting:

```bash
ngpt code --language python --plaintext "sort a list of integers"

```

Raw output suitable for direct piping or file redirection:

```python
def sort_numbers(nums):
    return sorted(nums)

```

### Programmatic Usage

You can also invoke the mode directly in Python:

```python
from ngpt.api.client import NGPTClient
from ngpt.cli.modes.code import code_mode

client = NGPTClient()  # Assumes API key is configured

args = {
    "language": "python",
    "prompt": "read a file and count lines",
    "plaintext": True,  # Key flag for markdown-free output

    "preprompt": None,
    # ... other CLI args ...

}

code_mode(client, args)  # Prints raw code without markdown fences

```

## Key Implementation Files

The markdown-free output functionality spans several critical files in the `nazdridoy/ngpt` repository:

| File | Role |
|------|------|
| `ngpt/cli/modes/code.py` | Core implementation of the code-generation command, handling flag parsing, prompt selection, and API invocation. |
| `ngpt/api/client.py` | Contains `NGPTClient.chat()` which respects the `markdown_format` parameter to suppress code fences. |
| `ngpt/ui/renderers.py` | Handles live Markdown rendering; bypassed when `--plaintext` is active. |
| `ngpt/ui/colors.py` | Supplies color constants for status messages and fallback warnings. |

## Summary

The ngpt code generation mode ensures clean, markdown-free output through a coordinated three-tier approach:

- **CLI Flag Control**: The `--plaintext` argument triggers the entire markdown-free pipeline.
- **Prompt Engineering**: Dedicated `CODE_SYSTEM_PROMPT_PLAINTEXT` templates explicitly instruct the LLM to omit formatting.
- **API Enforcement**: The `markdown_format=False` parameter passed to `NGPTClient.chat()` ensures the backend suppresses code fences.

These mechanisms work sequentially in `ngpt/cli/modes/code.py` to guarantee that when you request plain text, you receive raw, copy-paste-ready code without manual cleanup.

## Frequently Asked Questions

### What is the code generation mode in ngpt?

The code generation mode is a specialized CLI command (`ngpt code`) in the `nazdridoy/ngpt` repository designed specifically for generating programming code. Unlike the general chat mode, it supports language-specific context through the `--language` flag and offers dedicated output formatting options including the `--plaintext` flag for markdown-free results.

### How do I enable plain-text output when using ngpt code generation?

To receive raw code without Markdown formatting, append the `--plaintext` flag to your command: `ngpt code --language python --plaintext "your prompt here"`. This flag activates the plaintext system prompt and sets `markdown_format=False` in the API call, ensuring the response contains no code fences or syntax highlighting markers.

### Does using --plaintext affect how the code is displayed in my terminal?

No, the `--plaintext` flag specifically removes Markdown code fences (```) from the LLM's response, but it does not disable syntax highlighting performed by your terminal or pager. The raw code still contains valid Python (or other language) syntax that your terminal may colorize based on file extension or content detection, but you will not see the triple backticks or language identifiers introduced by Markdown formatting.

### Which source files control the markdown formatting behavior in ngpt?

The primary control resides in [`ngpt/cli/modes/code.py`](https://github.com/nazdridoy/ngpt/blob/main/ngpt/cli/modes/code.py), which handles the `--plaintext` flag and selects between `CODE_SYSTEM_PROMPT_MARKDOWN` and `CODE_SYSTEM_PROMPT_PLAINTEXT`. The [`ngpt/api/client.py`](https://github.com/nazdridoy/ngpt/blob/main/ngpt/api/client.py) file contains the `NGPTClient.chat()` method that accepts the `markdown_format` parameter to enforce formatting at the API level. Finally, [`ngpt/ui/renderers.py`](https://github.com/nazdridoy/ngpt/blob/main/ngpt/ui/renderers.py) manages the display layer, bypassing Markdown prettification when plaintext mode is active.