Study guide · Adding AI features via LLM APIs
Building AI Features with an LLM API: Prompts, Tools, RAG and Evals
Technically, adding an AI feature to your own app is often just one HTTP call. Making it reliable, safe and affordable in production takes more. On this page you learn how a call to an LLM API is structured and how to write good prompts. You learn how the model works with your app through tools and how to get reliable structured data. On top of that come streaming and error handling, costs and model choice, RAG with embeddings, agents and evals.
The example is once again the web app Invoicebox. The team adds three AI features. The first reads receipts from emails. The second is a support chat that answers questions using the help manual. The third is an assistant that handles simple tasks on its own.
How an API call is structured
A call sends the model a list of messages with roles. Whatever the person or the app writes goes under the role "user". The model's replies go under "assistant". On top of that comes the system prompt. It gives the model its role, rules and framework for the whole conversation, such as tone, format or limits.
The key point: a typical chat call is stateless. If the app calls the API twice in a row, the model knows nothing about the first call during the second one. For an ongoing conversation, the app resends the conversation so far with every request. The system prompt is also part of every request.
Models process text as a sequence of tokens. A token is a piece of text, often part of a word. Common words are one token, rare ones are split into several pieces. Context limits and billing are measured in tokens, not in characters or words.
Everything the model processes in one call counts against the context window. That includes the system prompt, the tool definitions and the conversation history sent along. The reply the model generates counts too. As a conversation grows, the app sooner or later has to trim or summarize it.
Two parameters appear in almost every API. The temperature controls how much randomness goes into choosing the next token. A low temperature makes the output more uniform and predictable. That suits extraction and classification, for example when invoice data from emails is copied into fixed fields. A higher temperature pays off when variety is wanted, such as when brainstorming ideas. Even at a low temperature, though, answers are not fully deterministic.
The second parameter is the maximum number of output tokens. If the reply reaches this limit, it stops right there, often mid-sentence. The API then reports a stop reason. Depending on the provider, the field is called "stop_reason" or "finish_reason", for example. It says why the model stopped: finished normally, hit the length limit, requested a tool, or refused the request. If you only read the reply text and ignore the stop reason, you show half sentences or forget to run a requested tool.
Writing good prompts
Models do not reliably guess what you mean. A specific prompt states scope, audience, tone and limits. "Write something about hiking boots" is vague. Better: "Write 3 sentences about hiking boots, audience beginners, factual, no superlatives." The result fits better and can be checked.
With few-shot prompting, you include a few examples of input and output in the prompt. Examples often show format and style more clearly than any description. But they should be varied. A single example is easily read as a template, and the model then copies the wording and length of exactly that example. So give several different examples and say what is binding, such as the format, and what may vary, such as content and length.
If the prompt processes outside text, keep instructions and data clearly apart. When Invoicebox has customer emails summarized, it states the instruction clearly and attaches the email set apart, for example in tags. That way the model can tell what is the task and what is the content to process. This improves results and makes it harder for text in the email to be mistaken for an instruction.
Tools: tool use and function calling
With tool use, also called function calling, the model can use functions of your app, for example to look up an order or check the weather. For this you describe each tool with three things: a name, a description of when and what it is for, and a schema of the input parameters. From these, the model decides whether and how to call the tool. Credentials never go into the definition. They stay in the app.
A tool you define yourself is always run by your own app, at the model's request. The flow:
- The app sends the request together with the tool definitions.
- The model ends its reply with a tool request: name, arguments and a call ID.
- The app checks the request and runs the function.
- The app sends the result back as its own message, referencing the call ID.
- The model writes the answer or requests another tool.
Without step 4, everything stalls. An Invoicebox developer adds a tool "get_weather". The model requests it, the app calls the weather API, and then nothing happens. The result is not sent back to the model as a tool result. Only then can the model write the actual answer.
If a tool fails, for example because an order number does not exist, the app sends back a tool result with an understandable error message. The model can then ask a follow-up question, correct the parameter or inform the user. A made-up success leads to wrong answers. Internal details such as stack traces do not belong in the context.
You can control whether the model uses tools. Common settings are: the model decides on its own, it must use some tool, a specific tool is forced, or tools are disabled for this call.
If the model often calls a tool in unsuitable situations, the most effective first step is a more precise description. It states the purpose, suitable and unsuitable cases, and what the parameters mean. Name and description are the main lever for good tool choice. Too many tools hurt as well.
The arguments of a tool call come from the model. They can be wrong because of mistakes or manipulated input. That is why the code checks permissions, plausibility and approvals. A description is only a hint to the model.