AI From Zero · AI for Developers

Sending Prompts Through an API

Learn how application prompts are prepared, transmitted through an AI API, and integrated into software workflows.

Estimated learning time: 45 minutes

What You'll Learn

  • Understand how a prompt moves from an application to an AI model through an API
  • Learn the difference between user input and application instructions
  • Understand how prompts can be constructed dynamically
  • Learn how application context can be included with a prompt
  • Understand why prompt size and relevance matter
  • Learn how developers can validate and control prompt inputs
  • Understand security and privacy considerations when sending prompts through an API

Introduction

In the previous lesson, you learned how an AI API request is structured. One of the most important pieces of that request is the information given to the model.

This information is commonly referred to as a prompt.

In a simple chatbot, a prompt may be the text typed by a user. In a real application, however, the prompt can be assembled from several sources, including user input, application instructions, business context, conversation history, and information retrieved from other systems.

This lesson explains how developers send prompts through an API and how prompt handling becomes part of application engineering.

1. What Is a Prompt?

A prompt is information provided to an AI model to guide or request a particular operation.

Depending on the model and API, a prompt may contain:

  • A question
  • An instruction
  • A document
  • Conversation messages
  • Examples
  • Relevant application context
  • Output requirements

The exact representation depends on the AI API being used.

2. From User Input to API Request

Consider a simple writing assistant.

The user enters:

Make this email more professional.

The application does not necessarily send that exact sentence directly to the model.

The backend may construct a more complete request that includes the user instruction, the text to be edited, application rules, and other relevant context.

The simplified flow is:

User Input → Application Logic → Prompt Construction → API Request → AI Model

3. User Input and Developer Instructions

One important distinction in AI applications is the difference between information supplied by the user and instructions established by the application.

User input represents what the user wants the system to process.

Application instructions define how the AI feature is expected to behave.

For example, a customer support application may instruct the model to use a professional tone and avoid making unsupported promises. The customer message is then supplied as the task input.

Keeping these responsibilities separate can make application behavior easier to control.

4. Constructing a Prompt

A developer can construct a prompt dynamically using information available to the application.

For example, an invoice analysis application might combine:

  • The analysis instruction
  • The invoice content
  • The required output format
  • Relevant business rules

The resulting request can then be sent to the AI API.

This means that the prompt is often generated by software rather than being manually typed by a developer each time.

5. A Conceptual Example

Suppose an application needs to classify customer feedback.

The application could conceptually construct information such as:

{
  "instruction": "Classify the customer feedback.",
  "categories": [
    "billing",
    "technical",
    "delivery",
    "general"
  ],
  "feedback": "The payment was charged twice."
}

The application then sends the appropriate representation of this information through the API.

The exact JSON structure will vary between AI services.

6. Prompt Construction Is Application Logic

Prompt construction should be considered part of the software application.

The developer decides:

  • What information should be included
  • What information should be excluded
  • Which instructions apply
  • How context is organized
  • What output is expected
  • What limits should be imposed

This means prompt engineering and software engineering often overlap in AI applications.

7. Adding Context

An AI model may need additional context to complete a task correctly.

For example, a company email assistant may need:

  • The original email
  • The intended audience
  • The desired tone
  • Company communication guidelines

The application can gather the required information and include it in the request.

However, developers should avoid sending irrelevant context simply because it is available.

8. Relevant Context Matters

More information does not automatically produce a better result.

Irrelevant or conflicting information can make a task harder for the model and can increase request size.

Good application design therefore focuses on providing the model with information that is relevant to the requested task.

9. Prompt Size

Prompts can become large, especially when applications include long documents or conversation histories.

Large prompts can affect:

  • Processing time
  • Usage limits
  • Cost
  • Response quality
  • Application performance

Developers should therefore consider how much information is actually necessary.

10. Conversation History

Chat applications often need to provide previous conversation context when generating a new response.

For example, if a user asks:

What about the second option?

the application may need previous messages to determine what the user means by second option.

The application is responsible for managing the conversation context according to the API and model architecture it uses.

11. Dynamic Prompts

Dynamic prompts are prompts assembled from changing application data.

For example, a sales assistant might construct a prompt using:

  • Customer information allowed for processing
  • Recent interaction history
  • Product information
  • Sales guidelines
  • The users current question

Each API request can therefore be different even though the application provides the same overall feature.

12. Templates

Developers can use prompt templates to create consistent requests.

A conceptual template might be:

Task:
[task description]

Relevant information:
[application data]

Requirements:
[output requirements]

User input:
[user content]

The application fills the appropriate sections with current information before sending the request.

Templates can make prompt construction easier to maintain and test.

13. Separating Instructions From Data

Applications should clearly distinguish instructions from data whenever the API architecture supports that distinction.

For example, an application can define the task separately from the customer message being analyzed.

This makes the intended behavior clearer and can help developers reason about how untrusted user content should be handled.

14. User Input Is Not Automatically Trusted

User-provided content can contain unexpected instructions or malicious content.

For example, a user might submit text that attempts to change how the application should behave.

Developers should therefore treat user input as untrusted data and design the application so that user content cannot automatically override critical application rules.

15. Prompt Injection

Prompt injection is a security concern in which untrusted content attempts to influence the behavior of an AI system in unintended ways.

This can occur when an application sends external content to an AI model and the content contains instructions that conflict with the applications intended behavior.

For example, a document being summarized could contain text telling the AI to ignore the original task.

Developers need to consider these risks when designing AI workflows.

16. Protecting Sensitive Data

Before sending a prompt, the application should determine whether the included information is appropriate to send to the AI service.

Developers should consider:

  • Personal information
  • Confidential business information
  • Authentication credentials
  • Financial information
  • Private documents
  • Internal security information

Sensitive information should only be included when there is a legitimate need and the applicable security and privacy requirements are satisfied.

17. Prompt Validation

Applications can validate user input before including it in an AI request.

Validation may include:

  • Maximum length checks
  • Required field checks
  • File type checks
  • Permission checks
  • Input normalization
  • Application-specific restrictions

Validation does not guarantee that all malicious content will be detected, but it provides an important layer of application control.

18. Output Requirements

A prompt can communicate how the application wants the AI result to be produced.

For example, an application may request:

  • A concise answer
  • A professional tone
  • A specific language
  • A list of items
  • A structured response
  • A classification from predefined categories

Later lessons will explore structured outputs in more detail.

19. Prompting Through an API

At a conceptual level, sending a prompt through an API involves the following steps:

  1. The user or application provides input.
  2. The application validates the input.
  3. The backend gathers relevant context.
  4. The application constructs the prompt or message structure.
  5. The backend creates the API request.
  6. Authentication information is added securely.
  7. The request is sent to the AI service.
  8. The selected model processes the prompt.
  9. The API returns the response.
  10. The application processes and validates the response.

This sequence is the foundation of many AI-powered features.

20. Example: AI Writing Assistant

Imagine a website that helps users improve business emails.

The user provides:

Please improve this email while keeping the original meaning.

The application could combine that instruction with:

  • The original email
  • A professional writing guideline
  • A maximum response length
  • A requirement not to invent facts

The resulting request is sent through the AI API.

When the response arrives, the application displays the suggested version to the user.

The user remains responsible for reviewing the final email before sending it.

21. Prompt Versioning

Prompts used in production applications may change over time.

Developers can maintain versions of important prompt templates so they can determine which version produced a particular behavior.

This becomes useful when testing improvements or investigating unexpected changes in application output.

22. Testing Prompts

A prompt that works well for one example may not work equally well for every input.

Developers should test prompts with a range of realistic cases.

Testing can include:

  • Normal inputs
  • Short inputs
  • Long inputs
  • Ambiguous inputs
  • Unexpected inputs
  • Boundary cases
  • Adversarial inputs

This helps reveal weaknesses before the feature is widely deployed.

23. Prompt Changes Can Change Application Behavior

A small change to an instruction can sometimes change model behavior.

For production applications, prompt changes should therefore be treated as meaningful software changes rather than casual text edits.

Important prompt changes can be documented, tested, reviewed, and monitored.

24. Cost Considerations

Prompt content contributes to the amount of information processed by an AI model.

Repeatedly sending large instructions, documents, or conversation histories can increase usage.

Developers can manage costs by keeping prompts focused, removing unnecessary context, selecting appropriate models, and monitoring usage.

25. Prompt Quality and Application Quality

A strong prompt is useful, but prompt quality alone does not make an application reliable.

A production AI feature also needs:

  • Secure authentication
  • Reliable API communication
  • Input validation
  • Output validation
  • Error handling
  • Privacy controls
  • Monitoring
  • Appropriate user experience

The prompt is one part of the overall application design.

26. A Useful Mental Model

Think of the prompt as the instruction and context layer between application logic and AI processing.

The application determines what information is appropriate and how it should be organized. The AI model then processes that information according to its capabilities.

This perspective helps developers avoid treating prompting as an isolated activity.

27. What Comes Next

The next lesson will focus on what happens after an AI API request has been sent.

You will learn how applications receive AI responses, interpret them, handle errors, validate results, and safely integrate those results into application workflows.

Conclusion

Sending prompts through an API is a core part of AI application development. Developers often construct prompts dynamically from user input, application instructions, and relevant context.

Reliable prompt handling requires more than writing good instructions. Developers must control what data is included, protect sensitive information, validate inputs, consider prompt injection, manage request size, test different cases, and monitor how prompt changes affect application behavior.

When prompting is treated as part of software engineering, AI capabilities can be integrated into applications in a more controlled, secure, and maintainable way.

Key Takeaways

• A prompt is information provided to an AI model to guide a task. • Applications can construct prompts dynamically from user input and application context. • User input and application instructions should be treated as separate concepts. • Relevant context is generally more useful than unnecessary information. • Large prompts can affect cost, latency, limits, and performance. • User input should be treated as untrusted data. • Prompt injection is an important security concern. • Sensitive information should only be sent when appropriate. • Prompt templates and versioning can improve consistency and maintainability. • Prompt quality is only one part of reliable AI application development.

Try It Yourself

Design a prompt template for an AI customer support assistant. Include a task instruction, relevant business context, customer input, and output requirements. Then identify three types of sensitive information that the application should avoid sending unless genuinely required.

Test Your Knowledge

You've reached the end of this lesson.

Test what you've learned with the Lesson 115 Quiz: Sending Prompts Through an API.

Take the Quiz
← Understanding AI API Requests
Handling AI Responses →
Back to Course