DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Intent Recognition: A Step-by-Step Guide

Intent recognition is the process of mapping user input to a predefined action or goal. Traditional approaches rely on rigid regex patterns or supervised classifiers that demand retraining for every new intent. Large language models handle this more gracefully. With the right prompt and a structured output schema, an LLM can classify user intent from a single example, adapt to new categories without fine-tuning, and reason through ambiguous phrasing. This guide walks through building a production-ready intent classifier using an LLM, from prompt design to handling edge cases, with concrete code you can run today.

Define Your Intent Taxonomy

Before writing prompts, lock down the set of intents you need and the boundaries between them. A good taxonomy is mutually exclusive and collectively exhaustive, at least for your domain. For a customer support bot, intents might include REFUND_REQUEST, TECHNICAL_SUPPORT, BILLING_QUESTION, and OUT_OF_SCOPE. Document two to three canonical utterances per intent. These will serve as few-shot examples in your prompt.

Be precise with naming. An intent called ACCOUNT_ISSUE is too broad, while PASSWORD_RESET and LOGIN_FAILURE are actionable. Clear boundaries reduce overlap and improve classifier consistency.

Select a Model and Infrastructure

For intent recognition, you need low latency, reliable JSON mode, and cost predictability. A mid-size reasoning model or general-purpose flagship is usually sufficient. You do not need the largest frontier model for classification.

Oxlo.ai is a strong fit here. It offers 45+ models across chat, reasoning, and code categories, all accessible through a fully OpenAI-compatible API. For intent classification, Qwen 3 32B provides solid multilingual reasoning, while Llama 3.3 70B works well as a general-purpose workhorse. If your prompts include long conversation histories or extensive few-shot examples, Oxlo.ai’s request-based pricing means your cost stays flat per call regardless of input length. That predictability matters when you are batch-processing thousands of conversations or running agentic workflows with large context windows. You can explore the details on the Oxlo.ai pricing page.

Design the Prompt and Schema

The prompt should separate instructions from examples. Use a system message to define the task, output format, and constraints. Then provide few-shot examples in user and assistant message pairs.

The schema should be strict. Ask for the intent label, a confidence score, and optional entities. JSON mode or function calling enforces this structure and removes the need to parse free text.

A minimal system prompt looks like this:

You are an intent classifier. Given a user message, respond with valid JSON containing:
- intent: one of the allowed values
- confidence: float between 0.0 and 1.0
- reasoning: one sentence explaining the classification

Allowed intents: REFUND_REQUEST, TECHNICAL_SUPPORT, BILLING_QUESTION, OUT_OF_SCOPE.

Keep the instruction list short. Adding too many constraints inside the system prompt can dilute the model's attention. Place your few-shot examples immediately after the system message so they sit close to the final user query in the context window.

Implementation

The following Python example uses the OpenAI SDK with Oxlo.ai as the provider. The code is a drop-in replacement: change the base_url to https://api.oxlo.ai/v1 and set your Oxlo.ai API key.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("OXLO_API_KEY"),
    base_url="https://api.oxlo.ai/v1"
)

INTENTS = [
    "REFUND_REQUEST",
    "TECHNICAL_SUPPORT",
    "BILLING_QUESTION",
    "OUT_OF_SCOPE"
]

system_prompt = """You are an intent classifier.
Given a user message, respond with valid JSON containing:
- intent: one of the allowed values
- confidence: float between 0.0 and 1.0
- reasoning: brief chain of thought explaining the classification

Allowed intents: REFUND_REQUEST, TECHNICAL_SUPPORT, BILLING_QUESTION, OUT_OF_SCOPE."""

few_shots = [
    {"role": "user", "content": "I want my money back, this product is broken."},
    {"role": "assistant", "content": '{"intent": "REFUND_REQUEST", "confidence": 0.98, "reasoning": "User explicitly requests money back due to defective product."}'},
    {"role": "user", "content": "How do I reset my password?"},
    {"role": "assistant", "content": '{"intent": "TECHNICAL_SUPPORT", "confidence": 0.97, "reasoning": "User asks for help with an account action."}'}
]

def classify_intent(user_message: str):
    messages = [
        {"role": "system", "content": system_prompt},
        *few_shots,
        {"role": "user", "content": user_message}
    ]

    response = client.chat.completions.create(
        model="qwen3-32b",
        messages=messages,
        response_format={"type": "json_object"},
        temperature=0.1,
        max_tokens=256
    )

    return response.choices[0].message.content

# Example usage
result = classify_intent("My last charge was double what I expected.")
print(result)

Use a low temperature, between 0.0 and 0.2, for classification tasks. Higher temperatures introduce unnecessary variation when the goal is deterministic routing.

Handle Edge Cases

Intent recognition fails gracefully only if you plan for it. Build handling logic for these common scenarios:

  • Low confidence: If the model returns a confidence score below your threshold, route the conversation to a human or ask a clarifying question rather than guessing.
  • Multi-intent: Some user messages contain two goals. Your schema can accept a list of intents, or you can add a secondary_intent field to capture the overlap.
  • <li

Top comments (0)