> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# When to Use an LLM

> Guidance on using code for deterministic tasks and LLMs for judgment and language-based tasks to reduce latency, cost, errors, and ensure reliable AI tool orchestration

You’ve just made your first API call. The model generated text, followed instructions, and responded to different temperatures. It’s impressive — and it’s tempting to reach for an LLM for every problem.

Don’t.

LLMs are powerful, but they aren’t the right tool for every task. Using one where ordinary code will do is like hiring a translator to read a stop sign: possible, but slow, costly, and unnecessary.

The key to building reliable, cost-effective AI systems is understanding when to call the model and when to just write code: the distinction determines latency, cost, reliability, and maintainability.

The key to building good AI applications, and eventually AI-driven systems,

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/z7NmHsFQN9LCEiD0/images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/dont-your-first-api-call-warning.jpg?fit=max&auto=format&n=z7NmHsFQN9LCEiD0&q=85&s=37263c1950935f45cadfaa62de8f6a9b" alt="A dark, grid-patterned graphic with retro pixelated text saying &#x22;YOUR FIRST API CALL&#x22; and a large yellow &#x22;DON'T.&#x22; Below it reads &#x22;Not the right tool for every job&#x22; and &#x22;Like hiring a translator to read a stop sign.&#x22;" width="1920" height="1080" data-path="images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/dont-your-first-api-call-warning.jpg" />
</Frame>

is knowing when to call the model and when to just write code.

Every task you encounter generally falls into one of two categories.

* Deterministic tasks — the same input always produces the same output. There is a single correct answer and you can write exact rules to compute it:

  * Calculate a tip
  * Sort a list
  * Convert a date format
  * Check if an email contains an `@` symbol

  These are best handled with a few lines of code: they run instantly, deterministically, and cheaply.

* Non-deterministic tasks — the right answer depends on judgment, context, or interpretation:

  * Summarize a legal document
  * Decide if a customer email sounds angry or confused
  * Write a professional reply to a complaint

  These require nuance, tone, and contextual understanding — exactly what LLMs are designed to handle.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/z7NmHsFQN9LCEiD0/images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/deterministic-vs-nondeterministic-tasks.jpg?fit=max&auto=format&n=z7NmHsFQN9LCEiD0&q=85&s=eb1127f20def24e3df16d2ad7fb22ccf" alt="An infographic titled &#x22;Two Kinds of Tasks&#x22; that contrasts deterministic tasks on the left with non-deterministic tasks on the right. The deterministic examples list actions like &#x22;calculate a tip,&#x22; &#x22;sort a list,&#x22; &#x22;convert a date,&#x22; and &#x22;check email format,&#x22; while the non-deterministic examples include &#x22;summarize a document&#x22; and &#x22;detect angry vs confused.&#x22;" width="1920" height="1080" data-path="images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/deterministic-vs-nondeterministic-tasks.jpg" />
</Frame>

Quick rule of thumb: if the answer is always the same for the same input, use code. If the answer requires judgment, context, or language understanding, use an LLM.

<Callout icon="lightbulb" color="#1CB2FE">
  Rule of thumb: Deterministic → code. Judgment/language → LLM.
</Callout>

Decision guide (examples):

| Task type | When to use code | When to use an LLM |
| - | - | - |
| Calculations | Deterministic math, conversions, validations | — |
| Text understanding | — | Summarization, sentiment, rewriting |
| Structured checks | Format or policy checks (e.g., `len(pw) >= 8`) | — |
| Extraction | Exact field parsing from known formats | Extracting names/titles from freeform email text |

Examples, side by side:

* Calculate shipping cost based on weight — code (deterministic).
* Summarize a product review — LLM (reading comprehension).
* Check if a password meets a minimum length — code:

```python theme={null}
len(pw) >= 8
```

* Decide if a review is positive or negative — LLM (requires interpretation).
* Convert Celsius to Fahrenheit — code (exact formula).
* Extract a name and job title from an email — LLM (names and formats vary).

Using an LLM when you don’t need one has tangible costs:

* It’s slower. An API call takes hundreds of milliseconds; a line of code runs in microseconds — roughly a thousandfold difference.
* It costs money. Every call consumes tokens and billing.
* It can be wrong. LLMs are probabilistic token predictors and may make arithmetic or factual errors.
* It can be unpredictable. Even with temperature set to 0, outputs may vary across calls or model versions.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/z7NmHsFQN9LCEiD0/images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/real-costs-slower-money-wrong-unpredictable.jpg?fit=max&auto=format&n=z7NmHsFQN9LCEiD0&q=85&s=325bff77267693f63bd54618b6b0a902" alt="A slide titled &#x22;REAL COSTS&#x22; with four colored boxes summarizing drawbacks: &#x22;Slower&#x22; (ms vs µs — 1000× difference), &#x22;Costs money&#x22; (every call uses tokens), &#x22;Can be wrong&#x22; (predicting tokens, not doing math), and &#x22;Unpredictable&#x22; (output can vary even at temp 0)." width="1920" height="1080" data-path="images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/real-costs-slower-money-wrong-unpredictable.jpg" />
</Frame>

<Callout icon="warning" color="#FF6B6B">
  Avoid unnecessary LLM calls: they add latency, cost, and potential inconsistency. Encapsulate deterministic logic in reliable functions and reserve LLMs for tasks that require judgment, context, or natural language.
</Callout>

Concrete example: building a receipt generator.

* The math — subtotal, tax, total — is deterministic and should be implemented in code.
* The friendly thank-you note at the bottom that varies in wording is a good fit for an LLM.

Deterministic code for receipt calculations:

```python theme={null}
subtotal = sum(prices)
tax = subtotal * 0.08
total = subtotal + tax
date = format(now())
```

When building agent-like AI systems, structure them as a set of tools (functions) that perform deterministic work: search a database, send an email, run calculations, validate inputs. Those tools should be reliable code. The LLM’s role is to interpret intent, decide which tool to call, and orchestrate higher-level behavior.

Design pattern summary:

* Tools = code (deterministic, testable, fast).
* LLM = decides, composes, interprets (judgment, context, natural language).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/z7NmHsFQN9LCEiD0/images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/looking-ahead-ai-agents-tools.jpg?fit=max&auto=format&n=z7NmHsFQN9LCEiD0&q=85&s=81a8ea05d07f0549918440fe64943176" alt="A retro-styled dark slide titled &#x22;LOOKING AHEAD&#x22; with the subtitle &#x22;AI agents will have tools.&#x22; It displays colorful button-like labels such as &#x22;SEARCH DATABASE,&#x22; &#x22;SEND EMAIL,&#x22; &#x22;RUN CALCULATION,&#x22; &#x22;TOOLS = CODE,&#x22; and &#x22;LLM = DECIDES.&#x22;" width="1920" height="1080" data-path="images/AI-Agents-for-Beginners-OpenClaw-Case-Study/LLM-Fundamentals/When-to-Use-an-LLM/looking-ahead-ai-agents-tools.jpg" />
</Frame>

Let the LLM think — and let code do the rest.

Further reading and references:

* [Best practices for using LLMs](https://platform.openai.com/docs/guides/gpt)
* [Designing reliable systems with LLMs and tools (agents)](https://ai.google/people/alik/agent-design)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/ai-agents-for-beginner-openclaw-case-study/module/13d4f7ad-29e5-4bc0-b026-47c4ae43c31c/lesson/52c32778-4793-4f2a-80a0-ff627d13832b" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.