LlmSendMessage Function  
 
BOOL WINAPI LlmSendMessage(
  HCLIENT hClient,  
  LPCTSTR lpszPrompt,  
  LPTSTR lpszResponse,  
  LPDWORD lpdwLength,  
  LPDWORD lpdwMessageId  
);

The LlmSendMessage function sends a prompt to the model and returns the response as a string.

Parameters

hClient
A handle to the client session.
lpszPrompt
A pointer to a null-terminated string which specifies the prompt which should be sent to the model. This parameter cannot be NULL or a zero-length string. If the string consists only of whitespace characters, or contains invalid Unicode or non-text data, this function will fail.
lpszResponse
A pointer to a string buffer which will contain the model's response to the prompt. The response will always be null terminated and if the buffer is not large enough to store the complete response, the response will be truncated. The lpdwLength parameter must be initialized with the maximum number of characters which can be copied into the buffer or the function will fail.
lpdwLength
A pointer to an unsigned integer which should be initialized to the size of the lpszResponse string buffer. When the function returns, this value will be updated with the actual number of characters copied into the buffer, not including the terminating null character. This parameter cannot be NULL and its initial value must be greater than zero.
lpdwMessageId
A pointer to an unsigned integer which will contain the ID for the message when the function returns. This parameter may be NULL if the application does not require the message ID.

Return Value

If the function succeeds, the return value is non-zero. If the function fails, the return value is zero. To get extended error information, call LlmGetLastError.

Remarks

The LlmSendMessage function provides a simplified interface for sending a single text prompt to the model and receiving the response as a string. This function is intended for applications that do not require structured input or detailed control over message formatting.

This function will not return until the request has completed, an error has occurred, or the operation has timed out. Applications that call this function from a user interface thread should provide visual feedback while waiting for the response, or perform the request on a background thread to maintain responsiveness.

Applications that accept user input should apply appropriate validation, filtering, or normalization before sending requests to the service provider. The LlmValidateText function verifies that the prompt contains valid message text and the LlmNormalizeText function can be used to normalize text formatting. These functions do not perform any semantic analysis of the prompt and do not provide protection against malicious or intentionally misleading input.

This function does not validate or restrict the content of the input message. It is the responsibility of the application to ensure that user-provided input is appropriate for its intended use. This includes considering scenarios where input may attempt to override instructions, inject unintended prompts, or otherwise influence the behavior of the model.

The response is written to the buffer specified by lpszResponse. If the buffer is not large enough to contain the complete response, the text will be truncated. The lpdwLength parameter will always contain the number of characters actually copied to the buffer.

Line breaks in the response text are normalized before being stored in the client history. Line feed and carriage return characters are converted to the standard Windows CRLF end-of-line sequence, ensuring consistent formatting across different providers and applications. This also helps ensure compatibility with Windows common controls and other user interface components.

Some models are capable of performing additional internal reasoning before generating a response. The amount of reasoning performed and whether it is exposed by the provider is model-specific. The library returns the final response intended for the user and does not currently expose model reasoning or intermediate processing steps. This behavior ensures consistent results across providers, regardless of how individual models implement or expose reasoning capabilities.

If the lpdwMessageId parameter is specified, it will receive a unique identifier for the message within the current client session. Message identifiers are assigned sequentially, but applications should treat them as opaque values. They are only guaranteed to be unique within a single session and should not be compared across different client sessions.

This function automatically adds the message to the conversation history for the session. Subsequent calls will include previous messages as context, subject to any configured history or token limits. To clear the conversation context, call LlmClearHistory or LlmResetSession.

Applications that require more advanced features, such as structured messages, role-based input, or access to detailed response metadata, should use the LlmSendMessageEx function instead.

Example

#include "cstools12.h"
#include <stdio.h>

int _tmain(void)
{
    HCLIENT hClient = INVALID_CLIENT;
    DWORD dwError = NO_ERROR;

    // Initialize the SocketTools library
    if (!LlmInitialize(CSTOOLS12_LICENSE_KEY, NULL))
    {
        _tprintf(_T("Initialization failed: %lu\n"), LlmGetLastError());
        return 1;
    }

    // Establish a connection to OpenAI using the default endpoint
    // and a lightweight model
    hClient = LlmConnect(
        LLM_PROVIDER_OPENAI,
        LLM_OPTION_DEFAULT,
        LLM_TIMEOUT,
        NULL,                   // Use default base URL
        _T("gpt-5.4-nano"),     // Model name
        _T("%OPENAI_API_KEY%")  // Provider API key
    );

    if (hClient == INVALID_CLIENT)
    {
        dwError = LlmGetLastError();
        _tprintf(_T("Unable to connect, error %lu\n"), dwError);
        LlmUninitialize();
        return 1;
    }

    TCHAR szResponse[4096] = {0};
    DWORD dwResponseLength = _countof(szResponse); // number of TCHARs
    DWORD dwMessageId = 0;

    // Send a simple message to the model
    if (LlmSendMessage(hClient,
                       _T("What is the capital city of Denmark?"),
                       szResponse,
                       &dwResponseLength,
                       &dwMessageId))
    {
        _tprintf(_T("Response:\n%s\n"), szResponse);
    }
    else
    {
        dwError = LlmGetLastError();
        _tprintf(_T("Request failed, error %lu\n"), dwError);
    }

    // Clean up
    LlmDisconnect(hClient);
    LlmUninitialize();

    return 0;
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csllmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also

LlmClearHistory, LlmGetMessageById, LlmGetTokenUsage, LlmNormalizeText, LlmResetSession, LlmSendMessageEx, LlmValidateText