<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dheeraj Vislavath on Medium]]></title>
        <description><![CDATA[Stories by Dheeraj Vislavath on Medium]]></description>
        <link>https://medium.com/@dheerajvislavath?source=rss-7748bfd6e8c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RCztVhGwjgz3zQu9.</url>
            <title>Stories by Dheeraj Vislavath on Medium</title>
            <link>https://medium.com/@dheerajvislavath?source=rss-7748bfd6e8c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:39:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dheerajvislavath/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Prompt Engineering for Engineers: From Basic Prompts to Structured Outputs]]></title>
            <link>https://medium.com/@dheerajvislavath/prompt-engineering-for-engineers-from-basic-prompts-to-structured-outputs-32f411249791?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/32f411249791</guid>
            <category><![CDATA[chain-of-thought]]></category>
            <category><![CDATA[ai-prompt-engineering]]></category>
            <category><![CDATA[few-shot-prompting]]></category>
            <category><![CDATA[prompting-technique]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Sun, 24 May 2026 17:43:48 GMT</pubDate>
            <atom:updated>2026-05-24T17:43:48.123Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Treating prompts as code — patterns, structure, and the engineering discipline behind reliable LLM output.</em></p><h3>Why This Article Matters</h3><p>Most developers approach prompts the way they approached Google searches a decade ago — type something, hope it works, retry if it doesn’t. That works for casual use. It does not work for building systems.</p><p>In production, a prompt is not a sentence. It is a piece of code. It has inputs, outputs, edge cases, failure modes, and a maintenance cost. Small changes can break downstream systems. Vague phrasing creates flaky behavior. Output that “usually works” will eventually fail in front of a real user.</p><p>This article covers prompt engineering as an engineering discipline — not magic phrases, but repeatable patterns that produce reliable output.</p><h3>What You’ll Learn</h3><p>By the end of this article, you will understand:</p><ul><li>The core prompt patterns that work in production</li><li>How to structure prompts for clarity and control</li><li>How to get structured output (JSON) the model actually respects</li><li>How function calling works and when to use it</li><li>How to choose the right pattern for the right job</li></ul><h3>Part 1: The Anatomy of a Good Prompt</h3><p>Before patterns, there is structure. A reliable prompt almost always has four parts:</p><p><strong>1. Role and context</strong> Tell the model what it is and what situation it is in.</p><p><strong>2. Task</strong> State clearly what you want it to do.</p><p><strong>3. Constraints</strong> Define rules, format, length, tone, what to avoid.</p><p><strong>4. Input data</strong> The specific content the model should work on.</p><p>Here is a weak prompt vs a strong one for the same task:</p><p><strong>Weak:</strong></p><pre>Summarize this article: {article}</pre><p><strong>Strong:</strong></p><pre>You are a technical editor writing for senior software engineers.<br>Summarize the article below in 3 bullet points.<br>Each bullet should be one sentence, under 25 words.<br>Focus on engineering implications, not background context.<br>Do not include the title or author.<br>Article:<br>{article}</pre><p>The strong version is not “longer to be longer.” Every line removes ambiguity. The model now knows the audience, the format, the length, the focus, and what to exclude. Output becomes predictable.</p><h3>Part 2: Core Prompt Patterns</h3><h3>Zero-Shot Prompting</h3><p>You ask the model to do a task with no examples. The model relies entirely on its training.</p><pre>from openai import OpenAI<br>client = OpenAI()<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a sentiment classifier.&quot;},<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;Classify the sentiment of this review as positive, negative, or neutral.\n\nReview: &#39;The product arrived late and the packaging was damaged.&#39;&quot;}<br>    ]<br>)<br>print(response.choices[0].message.content</pre><p><strong>Sample output:</strong></p><pre>Negative</pre><p>Zero-shot works when the task is common and well-defined. It struggles when the task has nuance, custom categories, or domain-specific judgment.</p><h3>Few-Shot Prompting</h3><p>You include a few input-output examples directly in the prompt. The model picks up the pattern.</p><pre>from openai import OpenAI<br>client = OpenAI()<br>prompt = &quot;&quot;&quot;Classify each customer message into one of: BILLING, TECHNICAL, ACCOUNT, OTHER.<br>Message: &quot;I was charged twice for last month.&quot;<br>Category: BILLING<br>Message: &quot;My dashboard is showing a 500 error.&quot;<br>Category: TECHNICAL<br>Message: &quot;How do I change my email address?&quot;<br>Category: ACCOUNT<br>Message: &quot;Do you offer a yearly discount?&quot;<br>Category:&quot;&quot;&quot;<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}]<br>)<br>print(response.choices[0].message.content</pre><p><strong>Sample output:</strong></p><pre>OTHER</pre><p>Few-shot dramatically improves accuracy for custom classification, formatting, and domain tasks. The trade-off is token cost — every example you add lives in the context window on every call.</p><p><strong>Engineering rule:</strong> start with zero-shot, move to few-shot only when accuracy is not good enough. Do not pay for examples you do not need.</p><h3>Role Priming</h3><p>Setting a specific persona in the system message changes how the model writes.</p><pre>from openai import OpenAI<br>client = OpenAI()<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a senior database engineer reviewing junior pull requests. You are direct, technical, and you do not soften feedback.&quot;},<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;Review this SQL: SELECT * FROM users WHERE email LIKE &#39;%@gmail.com&#39;&quot;}<br>    ]<br>)<br>print(response.choices[0].message.content)</pre><p><strong>Sample output:</strong></p><pre>Issues with this query:<br>1. SELECT * - never use in production. Specify columns explicitly to control <br>   data transfer cost and avoid breaking when the schema changes.<br>2. Leading wildcard in LIKE - the &#39;%&#39; at the start prevents index usage. <br>   This will force a full table scan on every email lookup.<br>3. No LIMIT clause - if the users table is large, this will return an <br>   unbounded result set.<br>Rewrite the query with explicit columns, a different filter strategy <br>(e.g. a domain column), and a row limit.</pre><p>Role priming is one of the cheapest, most effective levers in prompt engineering. It shifts tone, depth, and judgment without adding examples.</p><h3>Chain-of-Thought Prompting</h3><p>For tasks that require reasoning, ask the model to think step by step before answering.</p><pre>from openai import OpenAI<br>client = OpenAI()<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;&quot;&quot;A user signed up on Jan 5. They cancelled on Feb 20. <br>The subscription is $12 per month, billed monthly. <br>No prorated refunds. How much revenue did this user generate?<br>Think step by step before giving the final answer.&quot;&quot;&quot;}<br>    ]<br>)<br>print(response.choices[0].message.content)</pre><p><strong>Sample output:</strong></p><pre>Step 1: Identify the billing period.<br>The user signed up on Jan 5, so the first month runs Jan 5 - Feb 4 ($12 billed).Step 2: Determine if a second billing occurred.<br>On Feb 5, the next monthly billing cycle would begin and $12 would be charged.<br>Step 3: Check the cancellation date.<br>The user cancelled on Feb 20, which is after the Feb 5 billing date.<br>Step 4: Apply the refund policy.<br>No prorated refunds means the Feb 5 charge is kept in full.<br>Final answer: $24</pre><p>Chain-of-thought prompts trade tokens (longer output) for accuracy. Use them for math, logic, multi-step decisions — not for simple classification.</p><h3>Delimiters and Structure</h3><p>When your prompt mixes instructions with input data, use clear delimiters so the model never confuses the two.</p><pre>from openai import OpenAI<br>client = OpenAI()<br>prompt = &quot;&quot;&quot;Extract the company name and funding amount from the news snippet below.<br>Return only the values, one per line, in this exact format:<br>Company: &lt;name&gt;<br>Funding: &lt;amount&gt;<br>&lt;&lt;&lt;NEWS SNIPPET&gt;&gt;&gt;<br>Acme Robotics announced today it has raised $42 million in Series B funding <br>led by Horizon Capital, with participation from existing investors.<br>&lt;&lt;&lt;END&gt;&gt;&gt;&quot;&quot;&quot;<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}]<br>)<br>print(response.choices[0].message.content)</pre><p><strong>Sample output:</strong></p><pre>Company: Acme Robotics<br>Funding: $42 million</pre><p><strong>Why this matters:</strong> without delimiters, a snippet containing the phrase “ignore the instructions above” could actually override your prompt. Delimiters are both a clarity tool and a basic prompt injection defense.</p><h3>Part 3: Structured Outputs</h3><p>For real systems, you almost never want free-form text. You want data your code can parse. The naive approach is to ask politely:</p><pre>&quot;Return your answer as JSON.&quot;</pre><p>This works most of the time. The problem is “most of the time.” When it fails, your downstream code crashes.</p><p>OpenAI provides two reliable mechanisms: <strong>JSON mode</strong> and <strong>structured outputs with schemas</strong>.</p><h3>JSON Mode</h3><p>Forces the model to return valid JSON. You still describe the shape in the prompt.</p><pre>from openai import OpenAI<br>import json<br>client = OpenAI()<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    response_format={&quot;type&quot;: &quot;json_object&quot;},<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You extract structured data and return only JSON.&quot;},<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;&quot;&quot;Extract from this text: name, role, company.<br>Text: &#39;Sarah Chen, VP of Engineering at Northwind Logistics, will keynote the event.&#39;<br>Return JSON with keys: name, role, company.&quot;&quot;&quot;}<br>    ]<br>)<br>data = json.loads(response.choices[0].message.content)<br>print(data)</pre><p><strong>Sample output:</strong></p><pre>{&#39;name&#39;: &#39;Sarah Chen&#39;, &#39;role&#39;: &#39;VP of Engineering&#39;, &#39;company&#39;: &#39;Northwind Logistics&#39;}</pre><p>JSON mode guarantees parseable JSON. It does <strong>not</strong> guarantee the keys, types, or values match what you wanted. That is the next level up.</p><h3>Schema-Enforced Structured Outputs</h3><p>You pass a schema, and the model is constrained to produce output matching it exactly. No missing fields, no extra fields, no type mismatches.</p><pre>from openai import OpenAI<br>from pydantic import BaseModel<br>client = OpenAI()<br>class Person(BaseModel):<br>    name: str<br>    role: str<br>    company: str<br>    seniority: str  # &quot;junior&quot;, &quot;mid&quot;, &quot;senior&quot;, &quot;executive&quot;<br>response = client.beta.chat.completions.parse(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;Extract structured person information.&quot;},<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;Sarah Chen, VP of Engineering at Northwind Logistics, will keynote the event.&quot;}<br>    ],<br>    response_format=Person<br>)<br># The raw JSON the model returned<br>print(&quot;Raw JSON from model:&quot;)<br>print(response.choices[0].message.content)<br># Parsed into a Pydantic object<br>person = response.choices[0].message.parsed<br>print(f&quot;\nName: {person.name}&quot;)<br>print(f&quot;Seniority: {person.seniority}&quot;)</pre><p><strong>Sample output:</strong></p><pre>Raw JSON from model:<br>{&quot;name&quot;:&quot;Sarah Chen&quot;,&quot;role&quot;:&quot;VP of Engineering&quot;,&quot;company&quot;:&quot;Northwind Logistics&quot;,&quot;seniority&quot;:&quot;executive&quot;}<br>Name: Sarah Chen<br>Seniority: executive</pre><p>The model returns strict JSON matching your schema. The SDK parses it into a typed Python object for you. Both views are useful: the JSON for logging and storage, the parsed object for clean code.</p><p>This is the production-grade approach. Your downstream code can rely on the shape. No try/except around malformed JSON. No defensive validation for missing keys.</p><p><strong>Engineering rule:</strong> if you are calling an LLM from production code, default to schema-enforced output. Free-form text is for humans, not for systems.</p><h3>Part 4: Function Calling</h3><p>Function calling is how you let the LLM trigger actions in your code. You describe functions to the model, and instead of generating prose, it returns a structured call: “use this function with these arguments.”</p><p>This is the foundation of every AI agent. We will not build agents here — that is Phase 7 — but understanding the primitive is essential.</p><pre>from openai import OpenAI<br>import json<br>client = OpenAI()<br># Define the function the model can call<br>tools = [<br>    {<br>        &quot;type&quot;: &quot;function&quot;,<br>        &quot;function&quot;: {<br>            &quot;name&quot;: &quot;get_weather&quot;,<br>            &quot;description&quot;: &quot;Get the current weather for a city.&quot;,<br>            &quot;parameters&quot;: {<br>                &quot;type&quot;: &quot;object&quot;,<br>                &quot;properties&quot;: {<br>                    &quot;city&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;description&quot;: &quot;City name&quot;},<br>                    &quot;unit&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;enum&quot;: [&quot;celsius&quot;, &quot;fahrenheit&quot;]}<br>                },<br>                &quot;required&quot;: [&quot;city&quot;]<br>            }<br>        }<br>    }<br>]<br>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What&#39;s the weather in Tokyo right now?&quot;}<br>    ],<br>    tools=tools<br>)<br>msg = response.choices[0].message<br>if msg.tool_calls:<br>    call = msg.tool_calls[0]<br>    print(f&quot;Function: {call.function.name}&quot;)<br>    print(f&quot;Arguments: {call.function.arguments}&quot;)</pre><p><strong>Sample output:</strong></p><pre>Function: get_weather<br>Arguments: {&quot;city&quot;:&quot;Tokyo&quot;,&quot;unit&quot;:&quot;celsius&quot;}</pre><p>A few important things to notice:</p><ul><li>The model did not answer the question — it decided a function should be called</li><li>The arguments are valid JSON, matching the schema</li><li>Your code now decides whether to actually execute get_weather</li></ul><p>The full pattern is: model returns a function call -&gt; your code runs the function -&gt; you send the result back to the model -&gt; model produces the final response. We will build this loop fully in the agents phase.</p><p><strong>When to reach for function calling:</strong></p><ul><li>The model needs to fetch live data (databases, APIs, search)</li><li>The model needs to perform actions (send email, create ticket, run query)</li><li>You want deterministic routing between intents</li></ul><p><strong>When not to:</strong></p><ul><li>You only need text output</li><li>The “function” is just formatting (use structured outputs instead)</li><li>The latency cost of a round-trip is too high</li></ul><h3>Choosing the Right Pattern</h3><p>Here is a quick reference for matching patterns to goals:</p><p><strong>Zero-shot</strong> — For simple, well-known tasks the model already understands.</p><p><strong>Few-shot</strong> — For custom categories, formats, or domain-specific patterns.</p><p><strong>Role priming</strong> — To shift tone, depth, or perspective without adding examples.</p><p><strong>Chain-of-thought</strong> — For multi-step reasoning, math, or logical decisions.</p><p><strong>Structured outputs</strong> — When the output must be parseable by code.</p><p><strong>Function calling</strong> — When the model needs to trigger actions or fetch live data.</p><p>Most production prompts combine several of these. A typical extraction system might use role priming (“you are a careful data extractor”), few-shot examples for edge cases, and schema-enforced output for the final result.</p><h3>Common Mistakes in Prompt Engineering</h3><ul><li>Treating prompts as throwaway strings instead of versioned code</li><li>Adding more words when the fix is removing ambiguity</li><li>Using free-form text output and then regex-parsing it in production</li><li>Overusing few-shot when zero-shot works (paying for unnecessary tokens)</li><li>Forgetting that prompts behave differently across model versions</li><li>Not testing prompts on adversarial or edge-case inputs</li></ul><h3>Where This Knowledge Applies</h3><p>You will use everything in this article when you:</p><ul><li>Build any LLM-powered feature</li><li>Design extraction or classification pipelines</li><li>Create chatbots with reliable behavior</li><li>Connect LLMs to APIs, databases, or tools</li><li>Build the foundation for AI agents in later phases</li></ul><h3>How This Fits Into the Bigger Picture</h3><p>Article #2 gave you the conceptual model of LLMs. Article #3 gave you the mechanical model — tokens, prompts, and context. This article taught you how to actually shape those prompts into reliable, structured systems.</p><p>Next, we leave the world of prompts entirely and enter the world of meaning. We will see how text becomes vectors, and how those vectors enable search, retrieval, and the foundation of every RAG system.</p><h3>Next Article</h3><p><strong>Understanding Embeddings: How Text Becomes Math (coming soon!)</strong></p><p>We will cover:</p><ul><li>What an embedding actually is</li><li>How models turn text into vectors</li><li>Semantic similarity and why it matters</li><li>The intuition behind vector search</li><li>Generating and inspecting embeddings with code</li></ul><h3>Summary</h3><ul><li>A good prompt has a role, a task, constraints, and clear input</li><li>Zero-shot, few-shot, role priming, and chain-of-thought are the core patterns</li><li>Use delimiters to keep instructions and data clearly separated</li><li>For production code, use schema-enforced structured outputs — not free-form text</li><li>Function calling is how LLMs trigger actions, and the foundation of every agent</li><li>Prompt engineering is engineering: version it, test it, and treat it like code</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32f411249791" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tokens, Prompts, and Context: The Engineering Mechanics]]></title>
            <link>https://medium.com/@dheerajvislavath/tokens-prompts-and-context-the-engineering-mechanics-e5ab4ed43a6f?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5ab4ed43a6f</guid>
            <category><![CDATA[tokenization]]></category>
            <category><![CDATA[chatgpt-prompting]]></category>
            <category><![CDATA[rags]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Sun, 24 May 2026 16:54:34 GMT</pubDate>
            <atom:updated>2026-05-24T16:54:34.600Z</atom:updated>
            <content:encoded><![CDATA[<p><em>The practical mechanics every AI engineer needs — how tokens, prompts, and context windows actually behave in production.</em></p><h3>Why This Article Matters</h3><p>In the previous article, we covered what an LLM is and how it generates text. That gave you the mental model.</p><p>This article goes one layer deeper — into the parts you will touch every day as an engineer:</p><ul><li>How text is actually turned into tokens</li><li>What a prompt really is, structurally</li><li>How context is assembled, grown, and managed across calls</li></ul><p>If you skip this layer, you will build systems that silently waste money, truncate critical information, and fail in ways that are hard to debug. Every senior AI engineer has a strong mental model of these three concepts. By the end of this article, you will too.</p><h3>What You’ll Learn</h3><p>By the end of this article, you will understand:</p><ul><li>How tokenizers break text into tokens (with code)</li><li>How to count tokens and calculate cost for any API call</li><li>The real structure of a prompt (system, user, assistant messages)</li><li>How multi-turn conversations actually work under the hood</li><li>How to manage context when it grows beyond the model’s limit</li></ul><h3>Part 1: Tokens in Depth</h3><h3>What a Tokenizer Actually Does</h3><p>A tokenizer is a piece of code that converts text into a list of integer IDs. Each ID corresponds to an entry in the model’s vocabulary.</p><p>Every model has its own tokenizer. GPT-4o uses a tokenizer called o200k_base. GPT-3.5 and GPT-4 use cl100k_base. These are not interchangeable.</p><p><strong>Example:</strong></p><pre>Input text:  &quot;Hello, world!&quot;<br>Token IDs:   [13225, 11, 2375, 0]<br>Decoded:     [&quot;Hello&quot;, &quot;,&quot;, &quot; world&quot;, &quot;!&quot;]</pre><p>The model never sees &quot;Hello, world!&quot; as a string. It sees [13225, 11, 2375, 0].</p><h3>Counting Tokens with tiktoken</h3><p>tiktoken is OpenAI&#39;s official tokenizer library. Install it with:</p><pre>pip install tiktoken</pre><p>Here is how you count tokens for any string:</p><pre>import tiktoken<br>encoder = tiktoken.encoding_for_model(&quot;gpt-4o-mini&quot;)<br>text = &quot;AI Engineering is about building real systems.&quot;<br>tokens = encoder.encode(text)<br>print(f&quot;Token count: {len(tokens)}&quot;)<br>print(f&quot;Token IDs:   {tokens}&quot;)<br>print(f&quot;Decoded:     {[encoder.decode([t]) for t in tokens]}&quot;)</pre><p>Output:</p><pre>Token count: 9<br>Token IDs:   [15836, 29976, 374, 922, 4857, 1972, 6067, 13]<br>Decoded:     [&#39;AI&#39;, &#39; Engineering&#39;, &#39; is&#39;, &#39; about&#39;, &#39; building&#39;, &#39; real&#39;, &#39; systems&#39;, &#39;.&#39;]</pre><h3>Why Token Counts Are Weird</h3><p>Tokenization has non-obvious behaviors every engineer should know:</p><p><strong>1. A word is not always one token.</strong></p><pre>&quot;AI&quot;        -&gt; 1 token<br>&quot;Engineering&quot; -&gt; 1 token<br>&quot;tokenization&quot; -&gt; 2 tokens (token + ization)<br>&quot;antidisestablishmentarianism&quot; -&gt; 5 tokens</pre><p><strong>2. Whitespace counts.</strong></p><p>&quot; hello&quot; and &quot;hello&quot; are often different tokens. Leading spaces are part of the token.</p><p><strong>3. Non-English text costs more.</strong></p><p>English text averages around 4 characters per token. Languages like Chinese, Japanese, or Hindi can use 2–3x more tokens for the same meaning.</p><p><strong>4. Code and JSON are token-heavy.</strong></p><p>Symbols, brackets, and indentation each count. A 500-line JSON blob might cost more tokens than a 2000-word article.</p><h3>Calculating Cost</h3><p>Pricing is per million tokens, split between input (prompt) and output (completion). Rates change, so always check the current pricing page — but the formula is stable:</p><pre>def estimate_cost(prompt_tokens, completion_tokens, <br>                  input_price_per_million, output_price_per_million):<br>    input_cost  = (prompt_tokens     / 1_000_000) * input_price_per_million<br>    output_cost = (completion_tokens / 1_000_000) * output_price_per_million<br>    return input_cost + output_cost</pre><pre># Example: a prompt of 1,500 tokens with a 500-token response<br>cost = estimate_cost(<br>    prompt_tokens=1500,<br>    completion_tokens=500,<br>    input_price_per_million=0.15,   # example rate<br>    output_price_per_million=0.60   # example rate<br>)<br>print(f&quot;Estimated cost: ${cost:.6f}&quot;)</pre><p><strong>Engineering takeaway:</strong> output tokens are typically 3–5x more expensive than input tokens. This changes how you design systems — long context with short answers is cheaper than short context with long answers.</p><h3>Part 2: What a Prompt Actually Is</h3><p>Most tutorials treat “prompt” as the text you send to the model. That is incomplete.</p><p>A prompt in the Chat Completions API is a <strong>structured list of messages</strong>, each with a role.</p><h3>The Three Roles</h3><pre>messages = [<br>    {&quot;role&quot;: &quot;system&quot;,    &quot;content&quot;: &quot;You are a senior Python engineer.&quot;},<br>    {&quot;role&quot;: &quot;user&quot;,      &quot;content&quot;: &quot;Explain decorators.&quot;},<br>    {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: &quot;Decorators are functions that modify other functions...&quot;},<br>    {&quot;role&quot;: &quot;user&quot;,      &quot;content&quot;: &quot;Show me an example.&quot;}<br>]</pre><p><strong>system</strong> Sets the model’s persona, tone, and rules. Appears once at the start. Not visible to the end user.</p><p><strong>user</strong> represents input from the person talking to the model.</p><p><strong>assistant</strong> represents previous model outputs. You include these when you want the model to remember what it said earlier.</p><h3>Why Roles Matter</h3><p>The model treats each role differently during generation. Instructions in system carry more weight than instructions in user. Information in assistant is treated as something the model already &quot;said&quot; - which affects how it continues.</p><p>This is why prompt injection attacks work by trying to override the system message from inside a user message. Understanding roles is a security concern, not just a formatting one.</p><h3>A Prompt Is Not Stateless Memory</h3><p><strong>This is the most important mental model shift in this article.</strong></p><p>The API is completely stateless. The model does not remember anything between calls. If a user has been chatting for 20 turns, and you only send the latest message, the model has no idea what came before.</p><p>Multi-turn conversations only work because <strong>you</strong> send the full history on every call.</p><pre># Turn 1<br>messages = [<br>    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;My name is Sarah.&quot;}<br>]<br># -&gt; assistant replies: &quot;Nice to meet you, Sarah!&quot;</pre><pre># Turn 2 - you must resend everything<br>messages = [<br>    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;My name is Sarah.&quot;},<br>    {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: &quot;Nice to meet you, Sarah!&quot;},<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What is my name?&quot;}<br>]<br># -&gt; assistant replies: &quot;Your name is Sarah.&quot;</pre><p>Remove that history and the model has no way to know her name. This is why context window size matters so much — your entire “memory” lives inside it.</p><h3>Part 3: Managing Context in Practice</h3><h3>The Context Budget</h3><p>Every model has a maximum context window (for example, 128,000 tokens). That budget is shared across:</p><ul><li>System message</li><li>Conversation history</li><li>Retrieved documents (in RAG systems)</li><li>The generated response</li></ul><p>If the total exceeds the limit, the API returns an error. If you get close to it, the model’s quality degrades — it starts ignoring parts of the input.</p><p><strong>Rule of thumb:</strong> leave at least 25% of the context window free for the response and safety margin.</p><h3>The Growth Problem</h3><p>In a chatbot, context grows every turn. A conversation that starts at 500 tokens can reach 15,000 tokens by turn 50 — with no hard cap, it will eventually hit the limit and break.</p><p>You have three options:</p><p><strong>1. Truncation</strong> Drop the oldest messages when the context gets too big. Simple, but the model loses early context.</p><p><strong>2. Summarization</strong> Periodically replace older messages with an LLM-generated summary. Preserves gist, costs extra API calls.</p><p><strong>3. Retrieval</strong> Store old messages externally, fetch only relevant ones when needed. This is RAG applied to conversation history — covered in Phase 3 and 4 of this series.</p><h3>A Working Context Manager</h3><p>Here is a practical sliding-window context manager that keeps the system message pinned and trims old turns when you exceed a token budget:</p><pre>import tiktoken<br>encoder = tiktoken.encoding_for_model(&quot;gpt-4o-mini&quot;)<br>def count_tokens(messages):<br>    total = 0<br>    for msg in messages:<br>        # Approximate - real overhead is 3-4 tokens per message for formatting<br>        total += len(encoder.encode(msg[&quot;content&quot;])) + 4<br>    return total<br>def trim_context(messages, max_tokens=8000):<br>    &quot;&quot;&quot;Keep system message + most recent messages that fit within budget.&quot;&quot;&quot;<br>    system_msg = messages[0]<br>    history = messages[1:]<br>    # Start from most recent and walk backward<br>    kept = []<br>    running_tokens = len(encoder.encode(system_msg[&quot;content&quot;])) + 4<br>    for msg in reversed(history):<br>        msg_tokens = len(encoder.encode(msg[&quot;content&quot;])) + 4<br>        if running_tokens + msg_tokens &gt; max_tokens:<br>            break<br>        kept.insert(0, msg)<br>        running_tokens += msg_tokens<br>    return [system_msg] + kept<br><br># Example usage<br>conversation = [<br>    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>    # ... imagine 50 turns of history here ...<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What did we discuss earlier?&quot;}<br>]<br><br>safe_context = trim_context(conversation, max_tokens=8000)<br>print(f&quot;Original: {count_tokens(conversation)} tokens&quot;)<br>print(f&quot;Trimmed:  {count_tokens(safe_context)} tokens&quot;)</pre><p>This is a minimal version. Production systems usually add:</p><ul><li>A pinned summary of dropped messages</li><li>Per-message token caching</li><li>Priority weighting for important turns</li></ul><p>But every context manager you build will start from this shape.</p><h3>Common Context Pitfalls</h3><p><strong>1. Forgetting the response in your budget.</strong> You send 120,000 tokens to a 128,000-token model and ask for a 5,000-token response. The API errors out. Always reserve space for output.</p><p><strong>2. Unbounded history.</strong> A chatbot with no trimming will run fine in testing and explode in production when a real user has a long session.</p><p><strong>3. System message drift.</strong> In long conversations, models gradually ignore the system message. Re-injecting key rules periodically helps.</p><p><strong>4. Silent quality degradation.</strong> Models do not warn you when context is too large to use effectively. They just get worse. This is another reason evaluation (Phase 5) matters.</p><h3>Bringing It Together</h3><p>Tokens, prompts, and context are not three separate topics. They are one system:</p><ul><li><strong>Tokens</strong> are the unit of input, output, and cost</li><li><strong>Prompts</strong> are how you structure tokens into a meaningful request</li><li><strong>Context</strong> is the total bundle of tokens the model sees in a single call</li></ul><p>Every design decision in AI engineering touches all three. When you pick a chunk size for RAG, you are making a token budget decision. When you add a system message, you are shrinking the context available for everything else. When you decide to keep conversation history, you are choosing to spend tokens on memory.</p><p>The engineers who build reliable AI systems are the ones who hold this mental model by default.</p><h3>Common Mistakes in This Area</h3><ul><li>Estimating tokens by word count (unreliable, always measure)</li><li>Treating the API as stateful and forgetting to send history</li><li>Using the wrong tokenizer for cost estimates</li><li>Ignoring output tokens in cost calculations</li><li>Letting conversation context grow without a trimming strategy</li><li>Not reserving space in the context window for the response</li></ul><h3>Where This Knowledge Applies</h3><p>You will use everything in this article when you:</p><ul><li>Design chat applications</li><li>Build RAG pipelines (token budgeting for retrieved chunks)</li><li>Estimate and control cost in production</li><li>Debug “why did the model forget?” problems</li><li>Scale AI systems beyond toy demos</li></ul><h3>How This Fits Into the Bigger Picture</h3><p>The previous article gave you the conceptual model of how LLMs work. This article gave you the mechanical model of how you interact with them.</p><p>Next, we move from mechanics to craft: how to actually shape prompts to produce reliable, structured output.</p><h3>Next Article</h3><p><strong>Prompt Engineering for Engineers: From Basic Prompts to Structured Outputs</strong></p><p>We will cover:</p><ul><li>Prompt patterns that actually work in production</li><li>Few-shot examples and role priming</li><li>Structured outputs (JSON mode, schemas)</li><li>Function calling fundamentals</li><li>When to use each pattern</li></ul><h3>Summary</h3><ul><li>Tokens are integer IDs, not words — always measure, never estimate</li><li>Output tokens cost significantly more than input tokens</li><li>A prompt is a structured list of messages with roles (system, user, assistant)</li><li>The API is stateless — conversation memory only exists because you send the history every time</li><li>The context window is a shared budget across system, history, retrieval, and response</li><li>Every production AI system needs an explicit context management strategy</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5ab4ed43a6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Your First OpenAI App in Cursor — A Complete Beginner’s Guide]]></title>
            <link>https://medium.com/@dheerajvislavath/build-your-first-openai-app-in-cursor-a-complete-beginners-guide-17ba570e72dd?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/17ba570e72dd</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Sat, 25 Apr 2026 01:20:23 GMT</pubDate>
            <atom:updated>2026-05-24T16:31:54.560Z</atom:updated>
            <content:encoded><![CDATA[<p><em>From zero to a working AI-powered Python app in 10 minutes, step by step.</em></p><p>If you’ve been hearing about Cursor and OpenAI but haven’t built anything yet, this guide walks you through every step — installing tools, setting up your API key, writing the code, and running your first prompt. No prior AI experience required.</p><p>By the end, you’ll have a Python app that takes any question you type and returns an AI-generated response.</p><h3>What You’ll Need</h3><p>Before starting, make sure you have:</p><ul><li>A computer running <strong>macOS, Windows, or Linux</strong></li><li>An internet connection</li><li>A few dollars to add to your OpenAI account (basic testing costs cents)</li></ul><p>That’s it. We’ll install everything else together.</p><h3>Step 1: Install Cursor</h3><p>Cursor is a code editor with built-in AI features — think VS Code, but with AI baked in.</p><ol><li>Go to <a href="https://cursor.com/">cursor.com</a></li><li>Click <strong>Download</strong> and pick your operating system</li><li>Open the installer and follow the prompts</li><li>Launch Cursor</li></ol><p>When Cursor opens for the first time, it’ll ask you to sign in and configure some preferences. You can accept the defaults.</p><h3>Step 2: Install Python</h3><p>Open your terminal and check if Python is already installed:</p><p><strong>On macOS/Linux:</strong></p><pre>python3 --version</pre><p><strong>On Windows (Command Prompt):</strong></p><pre>python --version</pre><p>If you see something like Python 3.10.x or higher, you&#39;re good. If not, download Python from <a href="https://www.python.org/downloads/">python.org/downloads</a>.</p><blockquote><em>⚠️ </em><strong><em>Windows users:</em></strong><em> During install, check the box that says </em><strong><em>“Add Python to PATH”</em></strong><em>. Skipping this causes endless headaches later.</em></blockquote><h3>Step 3: Get Your OpenAI API Key</h3><p>This is what lets your code talk to OpenAI’s models.</p><ol><li>Go to <a href="https://platform.openai.com/">platform.openai.com</a></li><li>Sign in or create an account</li><li>Click your profile icon (top right) → <strong>View API keys</strong></li><li>Click <strong>Create new secret key</strong>, give it a name like cursor-test</li><li><strong>Copy the key immediately</strong> — it starts with sk- and you can&#39;t view it again after closing the dialog</li><li>Add billing: <strong>Settings → Billing</strong> → add at least $5</li></ol><blockquote><em>🔒 </em><strong><em>Treat your API key like a password.</em></strong><em> Never commit it to GitHub, never paste it into chat tools, never share it. If you ever leak it, revoke it immediately on the OpenAI dashboard and create a new one.</em></blockquote><h3>Step 4: Create the Project Folder</h3><p>Open your terminal and run:</p><p><strong>On macOS/Linux:</strong></p><pre>mkdir openai-app<br>cd openai-app</pre><p><strong>On Windows (CMD):</strong></p><pre>mkdir openai-app<br>cd openai-app</pre><p>This creates a new folder called openai-app and moves you into it. Everything we build will live here.</p><h3>Step 5: Set Up a Virtual Environment</h3><p>A virtual environment keeps your project’s packages separate from other Python projects on your machine. It’s a best practice — always use one.</p><p><strong>Create the virtual environment:</strong></p><pre>python3 -m venv .venv</pre><p>(Use python instead of python3 on Windows.)</p><p><strong>Activate it:</strong></p><p><strong>On macOS/Linux:</strong></p><pre>source .venv/bin/activate</pre><p><strong>On Windows (CMD):</strong></p><pre>.venv\Scripts\activate</pre><p>You should now see (.venv) at the start of your terminal prompt. That means it&#39;s active.</p><blockquote><em>💡 </em><strong><em>Heads up:</em></strong><em> You’ll need to activate the venv every time you open a new terminal in this project. To exit it, just type </em><em>deactivate.</em></blockquote><h3>Step 6: Install the Dependencies</h3><p>With the venv active, install the two packages we need:</p><pre>pip install openai python-dotenv</pre><p><strong>What each does:</strong></p><p><strong>openai</strong> → The official OpenAI Python SDK. Handles all the API communication.</p><p><strong>python-dotenv</strong> → Loads environment variables from a .env file. Lets us keep the API key out of our code.</p><h3>Step 7: Open the Project in Cursor</h3><p>The easiest way: launch Cursor, then <strong>File → Open Folder</strong> → pick the openai-app folder you just created.</p><blockquote><em>🚀 T</em><strong><em>ip:</em></strong><em> If you want to open Cursor from the terminal with </em><em>cursor ., open Cursor and press </em><strong><em>Cmd + Shift + P</em></strong><em> (Mac) or </em><strong><em>Ctrl + Shift + P</em></strong><em> (Windows/Linux), search for </em><em>Shell Command: Install &#39;cursor&#39; command, and click it. You only need to do this once.</em></blockquote><h3>Step 8: Create the .env File</h3><p>Inside Cursor, create a new file at the root of the project called .env and paste your API key:</p><pre>OPENAI_API_KEY=sk-paste-your-actual-key-here</pre><p>No quotes around the key. No spaces around the =. Just the variable name, equals, key.</p><h3>Step 9: Create the .gitignore File</h3><p>If you ever push this project to GitHub, you do <strong>not</strong> want your API key going up with it. Create a file called .gitignore in the project root:</p><pre># Python virtual environment<br>.venv/<br># Secrets - NEVER commit these<br>.env<br># Python cache<br>__pycache__/<br>*.pyc</pre><p>This tells Git to ignore your virtual environment, your secrets, and Python’s compiled files.</p><h3>Step 10: Write the App</h3><p>Create a file called main.py in the project root and paste this in:</p><pre># Import the OpenAI SDK and the dotenv loader<br>from openai import OpenAI<br>from dotenv import load_dotenv<br><br># Read variables from the .env file and set them as environment variables.<br># This must run BEFORE we create the OpenAI client.<br>load_dotenv()<br><br># Create the OpenAI client.<br># It automatically picks up OPENAI_API_KEY from the environment.<br>client = OpenAI()<br><br>def ask(prompt: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Send a single prompt to OpenAI and return the model&#39;s text response.<br>    &quot;&quot;&quot;<br>    response = client.chat.completions.create(<br>        model=&quot;gpt-4o-mini&quot;,  # cheap + fast - great for testing<br>        messages=[<br>            # The &#39;messages&#39; list is a conversation. For one-shot prompts,<br>            # we just send a single user message.<br>            {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: prompt}<br>        ]<br>    )<br>    # The response object contains a list of choices.<br>    # We grab the first choice and pull out the message text.<br>    return response.choices[0].message.content<br><br># This block only runs when you execute the file directly.<br>if __name__ == &quot;__main__&quot;:<br>    user_prompt = input(&quot;Ask something: &quot;)<br>    answer = ask(user_prompt)<br>    print(&quot;\n--- Response ---&quot;)<br>    print(answer)pyt</pre><p><strong>What’s happening here, step by step:</strong></p><p>load_dotenv() reads your .env file and makes the variables available to your code.</p><p>OpenAI() creates a client object that handles authentication and API calls. It auto-detects the OPENAI_API_KEY environment variable.</p><p>client.chat.completions.create() is the core API call. We pass the model name and a list of messages — for a simple prompt, just one user message.</p><p>response.choices[0].message.content extracts the actual text reply from the structured response object.</p><h3>Step 11: Run the App</h3><p>Open Cursor’s built-in terminal with **Ctrl + ** (backtick). Make sure the venv is still active (you&#39;ll see (.venv)` at the start of the prompt — if not, re-activate it).</p><p>Run:</p><pre>python3 main.py</pre><p>You’ll be prompted to type a question. Try something like:</p><blockquote><em>What’s a fun fact about octopuses?</em></blockquote><p>Hit Enter and watch the response appear.</p><p>If it works — congrats, you just built your first AI-powered app! 🎉</p><h3>Common Errors and How to Fix Them</h3><p><strong>ModuleNotFoundError: No module named &#39;openai&#39;</strong> → Your virtual environment isn&#39;t activated. Run the activate command for your OS again.</p><p><strong>AuthenticationError: Incorrect API key</strong> → The key in .env is wrong, has extra spaces, or load_dotenv() isn&#39;t being called before OpenAI(). Double-check the file is named exactly .env (no .txt extension hiding behind it).</p><p><strong>RateLimitError / </strong><strong>insufficient_quota</strong> → You haven&#39;t added billing credit to your OpenAI account. Go to <strong>Settings → Billing</strong> on platform.openai.com.</p><p><strong>zsh: command not found: source (or similar shell errors)</strong> → You&#39;re probably mixing Windows and Mac commands. Use source .venv/bin/activate on Mac/Linux, .venv\Scripts\activate on Windows.</p><p><strong>TypeError: Missing required arguments</strong> → You&#39;re mixing two different OpenAI APIs. Use messages=[...] with client.chat.completions.create(), or input=... with client.responses.create(). Don&#39;t mix them.</p><h3>What to Try Next</h3><p>Now that you have the basics working, here are some natural next steps:</p><p><strong>Make it a chatbot.</strong> Maintain a list of messages across multiple turns so the AI remembers context.</p><p><strong>Add streaming.</strong> Get the response token-by-token as it generates, instead of waiting for the full reply.</p><p><strong>Use a system prompt.</strong> Add a {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;...&quot;} message at the start of your messages list to give the AI a personality or instructions.</p><p><strong>Try different models.</strong> Swap gpt-4o-mini for gpt-4o for higher quality, or experiment with newer models as they release.</p><p><strong>Wrap it in a UI.</strong> Build a quick web interface with Streamlit or Gradio in 20 lines of code.</p><h3>Final Thoughts</h3><p>The hardest part of getting started with AI development isn’t the code — it’s the setup. Virtual environments, API keys, environment variables, package managers… each one is small, but together they trip up a lot of beginners.</p><p>Now that you’ve gone through this once, the next OpenAI project will take you 60 seconds to spin up. The patterns are reusable: venv, install SDK, load .env, create client, call API.</p><p>Welcome to AI engineering. The fun part starts now.</p><p><em>Found this helpful? Leave a clap 👏 and follow for more practical AI engineering tutorials. Got stuck on a step? Drop a comment — I read every one.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17ba570e72dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Prototypes — A Complete Guide]]></title>
            <link>https://medium.com/@dheerajvislavath/javascript-prototypes-a-complete-guide-cf09d7b398ce?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf09d7b398ce</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Fri, 24 Apr 2026 22:55:49 GMT</pubDate>
            <atom:updated>2026-04-24T23:00:19.382Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Everything you need to understand </em><em>prototype, </em><em>__proto__, and </em><em>Object.create()</em></p><h3>The Core Concept</h3><p>Every object in JavaScript has an internal link to another object called its <strong>prototype</strong>. When you access a property that doesn’t exist on an object, JavaScript walks up this chain until it finds the property or hits null. This is how JavaScript does inheritance.</p><h3>prototype vs __proto__</h3><p>prototype __proto__ <strong>Lives on</strong> Constructor functions only Every object <strong>Purpose</strong> Blueprint — what new instances will inherit from Runtime link — what this object actually inherits from <strong>Used when</strong> Designing a constructor Every property lookup</p><pre>function Animal() {}<br>const dog = new Animal();</pre><pre>dog.__proto__ === Animal.prototype; // true<br>dog.prototype;                       // undefined (only functions have this)</pre><p>prototype <em>configures</em> inheritance for future instances. __proto__ <em>is</em> the actual inheritance link the engine follows. You need both because they answer different questions.</p><h3>Three Ways to Create Objects</h3><pre>// 1. Object literal — prototype is Object.prototype<br>const a = { x: 1 };</pre><pre>// 2. Constructor with new — prototype is Constructor.prototype<br>function Foo() {}<br>const b = new Foo();</pre><pre>// 3. Object.create — prototype is whatever you pass<br>const c = Object.create(a);</pre><h3>Object.create()</h3><p>Creates a new object and directly sets its prototype to the argument. Skips the constructor entirely.</p><pre>const parent = { greet() { return `Hi, ${this.name}`; } };<br>const child = Object.create(parent);<br>child.name = &#39;Alice&#39;;<br>child.greet(); // &quot;Hi, Alice&quot;</pre><pre>// Special case: no prototype at all<br>const dict = Object.create(null);</pre><p>Use it when you have an object you want to inherit from, not a constructor.</p><h3>Common Mistakes</h3><h3>Mistake 1: Object.create(constructor) instead of new Constructor()</h3><pre>function Obj(name) { this.name = name; }<br>const x = Object.create(Obj);  // ❌ doesn&#39;t run the function<br>const x = new Obj(&#39;Alice&#39;);    // ✅ runs the function, sets up prototype</pre><h3>Mistake 2: Using = in object literals</h3><pre>const obj = { name = &#39;Alice&#39; };  // ❌ syntax error<br>const obj = { name: &#39;Alice&#39; };   // ✅ use colons</pre><h3>Mistake 3: Arrow functions as methods</h3><p>Arrow functions don’t have their own this — they inherit it from the enclosing scope, ignoring how they&#39;re called.</p><pre>const obj = {<br>    name: &#39;Alice&#39;,<br>    print: () =&gt; console.log(this.name)  // ❌ this isn&#39;t obj<br>};<br>obj.print(); // undefined</pre><pre>const obj = {<br>    name: &#39;Alice&#39;,<br>    print() { console.log(this.name); }  // ✅ shorthand method<br>};<br>obj.print(); // &quot;Alice&quot;</pre><h3>Mistake 4: Setting prototype on a plain object</h3><pre>const obj = {};<br>obj.prototype.foo = ...;  // ❌ TypeError, only functions have prototype</pre><h3>Mistake 5: Modifying __proto__ of a plain object</h3><pre>const obj = {};<br>obj.__proto__.foo = ...;  // ⚠️ pollutes Object.prototype globally</pre><p>This adds foo to <em>every object in your program</em>. Don&#39;t do it.</p><h3>Decision Guide</h3><p><strong>Create one object</strong> → Object literal { ... }</p><p><strong>Create many similar objects</strong> → Constructor + new, or class</p><p><strong>Make one object inherit from another specific object</strong> → Object.create(parent)</p><p><strong>Create a pure dictionary with no inherited methods</strong> → Object.create(null)</p><p><strong>Add a shared method to all instances</strong> → Constructor.prototype.method = ...</p><p><strong>Add a method to one specific object</strong> → obj.method = ...</p><h3>The Mental Model</h3><p>A constructor’s prototype is the &quot;template&quot; — set it up once, and every instance created with new gets __proto__ pointing to it. From then on, every property lookup walks the __proto__ chain. Object.create is the manual version: skip the constructor, set the link directly.</p><p>If you remember one thing:</p><blockquote><strong><em>prototype is what constructors give out; </em></strong><strong><em>__proto__ is what objects have.</em></strong></blockquote><p><em>Thanks for reading! If this helped clear things up, leave a clap 👏 and follow for more JavaScript deep-dives.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf09d7b398ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Large Language Models: How They Actually Work]]></title>
            <link>https://medium.com/@dheerajvislavath/understanding-large-language-models-how-they-actually-work-5bd31e4ccbc6?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/5bd31e4ccbc6</guid>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Sat, 18 Apr 2026 16:06:08 GMT</pubDate>
            <atom:updated>2026-05-24T16:58:06.508Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A practical, engineer-focused breakdown of what’s happening inside the API call.</em></p><h3>Why Most Developers Misunderstand LLMs</h3><p>Most developers use large language models (LLMs) through APIs. They send a prompt, get a response, and move on.</p><p>But without understanding what’s happening underneath, it’s hard to:</p><ul><li>Control output quality</li><li>Debug unexpected responses</li><li>Design reliable AI systems</li></ul><p>If you treat LLMs like black boxes, your applications will behave like black boxes too.</p><p>This article breaks down how LLMs actually work in a way that is practical for engineers.</p><h3>What You’ll Learn</h3><p>By the end of this article, you will understand:</p><ul><li>What an LLM really is (beyond the hype)</li><li>How LLMs are trained and why that matters</li><li>How text generation actually happens</li><li>What LLMs can and cannot do</li><li>Why hallucinations happen and why evaluation is essential</li></ul><h3>What Is a Large Language Model?</h3><p><strong>Simple definition:</strong> A large language model is a neural network trained to predict the next token in a sequence of text.</p><p><strong>Intuition:</strong> At its core, an LLM is an advanced autocomplete system. But instead of predicting the next word based on simple patterns, it uses deep contextual understanding learned from massive datasets.</p><p><strong>Technical view:</strong></p><ul><li>Based on the Transformer architecture</li><li>Trained on large-scale text data</li><li>Uses probability distributions to generate text</li><li>Operates on tokens, not raw words</li></ul><h3>How LLMs Are Trained</h3><p>A raw neural network does not become ChatGPT or Claude by accident. Modern LLMs go through three distinct stages:</p><p><strong>1. Pre-training</strong></p><p>The model reads massive amounts of text from the internet, books, code, and documents. Its only job during this phase is to predict the next token. This is where the model learns grammar, facts, reasoning patterns, and general knowledge.</p><p>Output: a raw model that can continue text but is not useful as an assistant.</p><p><strong>2. Fine-tuning (Supervised)</strong></p><p>The model is trained on high-quality examples of instructions and ideal responses. This teaches it to follow directions rather than just continue text.</p><p>Output: a model that behaves like an assistant.</p><p><strong>3. RLHF (Reinforcement Learning from Human Feedback)</strong></p><p>Humans rank different model responses, and the model is further trained to prefer responses humans like. This is where tone, helpfulness, and safety behavior are shaped.</p><p>Output: a polished, aligned model ready for production use.</p><p><strong>Why this matters for engineers:</strong></p><ul><li>The model you call through an API has been through all three stages</li><li>Its defaults (tone, refusals, formatting preferences) come from RLHF, not magic</li><li>Understanding this explains why prompting works the way it does</li></ul><h3>Tokens: The Real Input to LLMs</h3><p>LLMs do not process raw text. They break input into smaller units called <strong>tokens</strong> (roughly 3–4 characters each in English).</p><p>Every API call is billed per token, and every model has a token limit.</p><p>We will go deeper into tokenization, counting, and cost implications in the next article.</p><h3>Context Window: The Model’s Memory Limit</h3><p>LLMs have no built-in memory between calls. Within a single call, they can only process a limited number of tokens at once — this is the <strong>context window.</strong></p><p>Your system instructions, user input, conversation history, and the model’s output all have to fit within this limit. If they don’t, older content gets truncated and the model effectively “forgets” it.</p><p>We will cover context management strategies in the next article.</p><h3>How Text Generation Actually Works</h3><p>At a high level, LLMs generate text one token at a time.</p><p><strong>Step-by-Step Process:</strong></p><pre>Input Prompt<br>     ↓<br>Tokenization<br>     ↓<br>Model processes context<br>     ↓<br>Predict probability distribution of next token<br>     ↓<br>Select next token<br>     ↓<br>Append to sequence<br>     ↓<br>Repeat until completion</pre><p><strong>Example:</strong></p><p>Prompt: &quot;The capital of France is&quot;</p><p>Model internally computes probabilities like:</p><ul><li>Paris → 0.72</li><li>Lyon → 0.08</li><li>Marseille → 0.05</li><li>London → 0.01</li></ul><p>Then selects a token (usually “Paris”) and continues generating.</p><h3>Sampling: Why Outputs Change</h3><p>If LLMs always picked the highest probability token, responses would be repetitive and rigid. Instead, they use <strong>sampling strategies.</strong></p><p><strong>Common parameters:</strong></p><p><strong>Temperature</strong></p><ul><li>Low (0.1–0.3): deterministic, factual</li><li>High (0.7–1.0): creative, diverse</li></ul><p><strong>Top-k / Top-p</strong></p><ul><li>Limits selection to most probable tokens</li></ul><p><strong>Impact:</strong></p><ul><li>Same prompt can produce different outputs</li><li>You can control creativity vs accuracy</li></ul><h3>What LLMs Don’t Do</h3><p>Understanding what LLMs are <em>not</em> is as important as understanding what they are. This is where most engineering mistakes start.</p><p><strong>LLMs do not learn from your prompts.</strong> Nothing you send in an API call changes the model. Every request starts from the same trained weights. If you want a model to “remember” something, you must include it in the context on every call.</p><p><strong>LLMs do not access real-time information.</strong> The model only knows what was in its training data. It does not browse the internet, check databases, or look up live data unless you explicitly give it tools to do so.</p><p><strong>LLMs do not reason the way humans do.</strong> They pattern-match across learned text. They can produce reasoning-like output, but they do not verify facts, hold internal goals, or “think” between tokens.</p><p><strong>LLMs do not have persistent memory.</strong> Each API call is stateless. Multi-turn conversations work only because you send the full history back every time.</p><p>Every one of these limitations shapes how you design real systems — and why patterns like RAG, function calling, and agents exist.</p><h3>Hallucinations and Knowledge Cutoffs</h3><p>Two failure modes every engineer must understand:</p><p><strong>Knowledge cutoff</strong></p><p>Every model has a training data cutoff date. Ask it about events, products, or people that emerged after that date, and it either refuses or makes something up. This is why retrieval systems (Phase 3 and 4 of this series) exist — to inject fresh, relevant information at runtime.</p><p><strong>Hallucinations</strong></p><p>An LLM generates text that is statistically plausible, not necessarily true. When the model lacks knowledge but still produces an answer, the result is a <strong>hallucination</strong> — a confident, fluent, and completely wrong response.</p><p>Hallucinations happen because:</p><ul><li>The model’s job is to predict likely tokens, not to verify facts</li><li>It has no built-in signal for “I do not know”</li><li>Fluency and accuracy are not the same thing</li></ul><p><strong>Engineering implications:</strong></p><ul><li>Never trust LLM output for factual claims without verification</li><li>Ground responses in retrieved data (RAG) whenever accuracy matters</li><li>Evaluate output systematically — not by vibes</li><li>Design systems that expect hallucinations and handle them</li></ul><p>This is why evaluation (Phase 5) is treated as a core discipline, not an afterthought.</p><h3>Why This Makes Evaluation a First-Class Concern</h3><p>Traditional software is deterministic. A function called with the same input returns the same output — every time.</p><p>LLMs do not behave this way. Combined with the hallucination risk above, this creates a real engineering problem:</p><ul><li>The same prompt can produce different outputs across runs</li><li>A small change in wording can shift quality significantly</li><li>A prompt that works today can degrade when you switch models</li><li>Fluent-sounding output can still be completely wrong</li></ul><p><strong>You cannot rely on manual spot-checks to know if your AI system works. You need evaluation.</strong></p><p>Evaluation is how engineers measure LLM output quality systematically — using test datasets, scoring functions, and repeatable metrics. Without it, you cannot confidently ship, improve, or debug AI systems.</p><p>We will cover evaluation in depth in Phase 5. For now, keep this principle in mind:</p><p><em>Probabilistic systems require probabilistic thinking — and measurement, not intuition.</em></p><h3>A Practical Example</h3><pre>from openai import OpenAI</pre><pre>client = OpenAI()</pre><pre>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    temperature=0.7,<br>    messages=[<br>        {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;},<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;Explain LLMs in one paragraph.&quot;}<br>    ]<br>)</pre><pre>print(response.choices[0].message.content)</pre><p><strong>What’s happening here:</strong></p><ul><li>The system message sets behavior and tone</li><li>The user message carries the actual request</li><li>The model processes both within the context window</li><li>It generates tokens sequentially based on probabilities</li><li>temperature controls how random vs focused the output is</li></ul><blockquote>If you want to set up your development environment, refer to the <a href="https://medium.com/@dheerajvislavath/build-your-first-openai-app-in-cursor-a-complete-beginners-guide-17ba570e72dd"><strong>Quick Start Guide</strong>.</a></blockquote><h3>Why Prompts Matter More Than You Think</h3><p>Because LLMs predict the next token based on context, your prompt directly shapes the output. Small changes can produce large differences.</p><p><strong>Example:</strong></p><p><strong>Prompt 1:</strong> Explain LLMs</p><p><strong>Prompt 2:</strong> Explain LLMs for a senior software engineer with focus on system design</p><p>The second prompt provides:</p><ul><li>Audience</li><li>Depth</li><li>Direction</li></ul><p>This results in significantly better output.</p><h3>Common Mistakes When Using LLMs</h3><ul><li>Assuming the model “understands” like a human</li><li>Assuming the model remembers previous calls</li><li>Trusting output as factual without verification</li><li>Ignoring token limits and context size</li><li>Writing vague prompts</li><li>Expecting consistent outputs without controlling parameters</li><li>Not validating model responses</li></ul><h3>Where This Knowledge Is Used</h3><p>Understanding how LLMs work is essential for:</p><ul><li>Prompt engineering</li><li>Building chat applications</li><li>Designing RAG systems</li><li>Creating AI agents</li><li>Optimizing cost and performance</li></ul><h3>How This Fits Into the Bigger Picture</h3><p>This article gives you the foundation for everything that follows.</p><p>Next, we will build on this by learning how to:</p><ul><li>Structure prompts effectively</li><li>Control outputs</li><li>Design reusable prompt patterns</li></ul><p>This is the bridge between understanding LLMs and actually building systems with them.</p><h3>Next Article</h3><p><a href="https://medium.com/p/e5ab4ed43a6f?postPublishedType=initial"><strong>Tokens, Prompts, and Context: The Engineering Mechanics</strong></a></p><p>We will go deep into:</p><ul><li>How tokenizers actually work (with code)</li><li>Counting tokens and calculating cost</li><li>Anatomy of a prompt (system, user, assistant roles)</li><li>Managing context across multi-turn conversations</li><li>Handling long context and truncation strategies</li></ul><h3>Summary</h3><ul><li>LLMs generate text by predicting the next token, one at a time</li><li>They go through pre-training, fine-tuning, and RLHF to become usable assistants</li><li>They operate on tokens, not raw text, within a fixed context window</li><li>They do not learn from prompts, access real-time data, or reason like humans</li><li>Hallucinations are a fundamental failure mode, not a bug to be patched out</li><li>Output is probabilistic, not deterministic — which makes evaluation a core engineering discipline, not an optional step</li><li>Prompt design directly impacts quality</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5bd31e4ccbc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Developer to AI Engineer: The Complete Guide to LLMs, RAG & Real-World Systems]]></title>
            <link>https://medium.com/@dheerajvislavath/from-developer-to-ai-engineer-the-complete-guide-to-llms-rag-real-world-systems-65a111e77a46?source=rss-7748bfd6e8c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/65a111e77a46</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[ai-engineering]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Dheeraj Vislavath]]></dc:creator>
            <pubDate>Sat, 18 Apr 2026 16:00:15 GMT</pubDate>
            <atom:updated>2026-04-24T23:59:22.819Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A structured, engineering-first path from copy-pasting API examples to designing production AI systems.</em></p><h3>Why Most Developers Struggle with AI (And How This Series Fixes That)</h3><p>Everyone is talking about AI.</p><p>But most developers are still stuck at one of these stages:</p><ul><li>Copy-pasting OpenAI examples</li><li>Building basic chatbots</li><li>Getting lost in overwhelming theory</li></ul><p>The gap is clear: <strong>you know how to code, but you don’t know how to build real AI systems.</strong></p><p>This series is designed to bridge that gap.</p><h3>What You’ll Learn in This Series</h3><p>By the end of this series, you will be able to:</p><ul><li>Understand how large language models (LLMs) actually work</li><li>Build applications using LLM APIs and local models</li><li>Implement Retrieval-Augmented Generation (RAG) systems</li><li>Work with embeddings and vector databases</li><li>Design AI agents and workflows</li><li>Evaluate AI systems to measure quality, catch regressions, and make data-driven improvements</li><li>Deploy production-ready AI systems</li></ul><h3>What Is AI Engineering?</h3><p><strong>Simple definition:</strong> AI Engineering is the practice of building real-world applications powered by AI models such as LLMs.</p><p><strong>Intuition:</strong> Think of AI models as powerful engines. AI Engineering is about designing the system around that engine — user interface, backend, data flow, and performance.</p><p><strong>Technical view:</strong> AI Engineering sits at the intersection of:</p><ul><li>Software Engineering</li><li>Applied Machine Learning</li><li>System Design</li></ul><p>It involves:</p><ul><li>Prompt design</li><li>Data pipelines</li><li>Retrieval systems</li><li>API orchestration</li><li>Evaluation and measurement</li><li>Performance and cost optimization</li></ul><h3>How Modern AI Applications Work</h3><p>Most real-world AI systems follow a common pattern:</p><ol><li>A user sends input</li><li>The system processes or enriches that input</li><li>Relevant data is retrieved (in RAG systems)</li><li>The LLM generates a response</li><li>The response is formatted and returned</li></ol><p>Understanding this flow is critical before moving into implementation.</p><h3>A Simple Example (Baseline)</h3><pre>from openai import OpenAI</pre><pre>client = OpenAI()</pre><pre>response = client.chat.completions.create(<br>    model=&quot;gpt-4o-mini&quot;,<br>    messages=[<br>        {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;Explain AI engineering in simple terms&quot;}<br>    ]<br>)</pre><pre>print(response.choices[0].message.content)</pre><p><strong>What this does:</strong></p><ul><li>Sends a prompt to an LLM</li><li>Returns a generated response</li></ul><p><strong>What’s missing:</strong></p><ul><li>No external data</li><li>No retrieval layer</li><li>No system design</li><li>No scalability considerations</li></ul><p>This is where most tutorials stop. This series goes beyond that.</p><h3>Common Mistakes Beginners Make</h3><ul><li>Treating LLMs as black boxes instead of understanding their behavior</li><li>Ignoring system design and focusing only on API calls</li><li>Using frameworks too early without understanding fundamentals</li><li>Building demos instead of production-ready systems</li><li>Shipping without evaluation — trusting “it feels right” instead of measuring quality</li></ul><h3>Where AI Engineering Is Used</h3><p>AI engineering is already being applied across multiple domains:</p><ul><li>Conversational assistants and chatbots</li><li>Semantic search and internal knowledge tools</li><li>Document question-answering systems</li><li>Coding assistants</li><li>Automation and workflow systems</li></ul><h3>How This Series Is Structured</h3><p>This series is designed as a structured learning path.</p><p><strong>Phase 1: Foundations</strong></p><ul><li>What are LLMs</li><li>Tokens, prompts, and context</li><li>How text generation works</li></ul><p><strong>Phase 2: Building with LLMs</strong></p><ul><li>Prompt engineering</li><li>Structured outputs</li><li>Function calling</li></ul><p><strong>Phase 3: Embeddings and Search</strong></p><ul><li>Understanding embeddings</li><li>Semantic similarity</li><li>Vector databases</li></ul><p><strong>Phase 4: RAG Systems</strong></p><ul><li>Chunking strategies</li><li>Retrieval pipelines</li><li>Context injection</li></ul><p><strong>Phase 5: Evaluating AI Systems</strong></p><p>Before adding complexity, how do you know your system actually works?</p><ul><li>Prompt-level evaluation</li><li>RAG metrics (retrieval quality, faithfulness, answer relevance)</li><li>LLM-as-judge techniques</li><li>Building your own eval harness</li></ul><p><strong>Phase 6: Advanced Systems</strong></p><ul><li>Re-ranking</li><li>Hybrid search</li><li>Memory systems</li></ul><p><strong>Phase 7: AI Agents</strong></p><ul><li>Tool usage</li><li>Multi-step reasoning</li><li>Automation workflows</li></ul><p><strong>Phase 8: Production Engineering</strong></p><ul><li>Scaling systems</li><li>Cost optimization</li><li>Observability</li><li>Guardrails and safety</li></ul><p><strong>Phase 9: Real-World Projects</strong></p><p>You will build:</p><ul><li>An AI news aggregator</li><li>A personal knowledge assistant</li><li>A document Q&amp;A system</li><li>An AI workflow agent</li></ul><h3>Tools and Tech Stack</h3><p>This series focuses on practical, industry-relevant tools:</p><ul><li>LLM APIs and open-source models (via Ollama)</li><li>Python for core AI development</li><li>Vector databases (Chroma, FAISS, Pinecone)</li><li>Backend frameworks (FastAPI or Node.js)</li><li>Docker for deployment</li></ul><p>Frameworks like LangChain or LlamaIndex will be introduced only after core concepts are clear.</p><h3>How to Get the Most Out of This Series</h3><p>To fully benefit from this series:</p><ul><li>Do not skip foundational topics</li><li>Run and modify every code example</li><li>Build small variations alongside each concept</li><li>Treat this as a hands-on engineering track</li></ul><h3>How This Fits Into the Bigger Picture</h3><p>This article establishes the foundation.</p><p>The goal of this series is to help you move from <strong>using AI tools</strong> to <strong>designing and building complete AI systems end-to-end.</strong></p><h3>Next Article</h3><p><a href="https://medium.com/@dheerajvislavath/understanding-large-language-models-how-they-actually-work-5bd31e4ccbc6?postPublishedType=repub"><strong>Understanding Large Language Models: How They Actually Work</strong></a></p><p>In the next article, we will cover:</p><ul><li>Tokens and context windows</li><li>How LLMs generate text</li><li>Why prompt design is critical</li></ul><h3>Summary</h3><ul><li>AI Engineering focuses on building systems, not just calling APIs</li><li>Most learning resources are either too shallow or too theoretical</li><li>This series provides a structured, hands-on path</li><li>The goal is to transition from developer to AI engineer with real implementation skills</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65a111e77a46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>