Share this page

# llm_request

The llm_request function emulates an LLM (large language model) request. It tracks the number of tokens sent to, and received from, the LLM.

Note: Anthropic and custom LLM models are supported from version 26.3.

## C Language

    int llm_request(const char *RequestName, const char *URL=<myURL>, const char *AIModel=<my_ai_model>, const char *RequestTokens=<Path_to_the_request_token_number_in_json_response>, const char *ResponseTokens=<Path_to_the_response_token_number_in_json_response>, const bool CrossStep, LAST);

Argument | Description
--- | ---
RequestName | The title of the request.
URL | The URL of the API, for example `https://api.openai.com/v1/chat/completions`
AIModel | The AI model. Supported values: `openai`, `gemini`, `anthropic`, `custom`
RequestTokens | Required when AIModel is set to `custom`, otherwise it is ignored. The path in the response (in JSON format) for the request token count returned by the server.
ResponseTokens | Required when AIModel is set to `custom`, otherwise it is ignored. The path in the response (in JSON format) for the response token count returned by the server.
CrossStep | Controls whether the script waits for the LLM request to complete. If true, the steps can end without waiting for the server response and for the URL to complete. At the end of the iteration, if no data arrived until that time, the step is aborted without failing the script. Default: false, meaning the script waits for the response.

## Return Values

This function returns LR_PASS (0) on success, and LR_FAIL (1) on failure.

## Parameterization

The arguments can be parameterized using standard parameterization.

## General Information

It is recommended that you add headers before the LLM requests.

Use lr_eval_string to retrieve the body.

## Examples

### OpenAI model

Copy code

    web_add_header("Content-Type","application/json");
    web_add_header("Authorization","<API_KEY>");
    llm_request("OpenAI_Chat_Completion",
        "URL=https://api.openai.com/v1/chat/completions",
        "AIModel=openai",
       "Body={\"model\": \"gpt-4o\", \"messages\": [{\"role\": \"user\", \"content\": \"hello\"}], \"temperature\": 0.7}"),
       LAST);
    return 0;

### Gemini model

Copy code

    lr_param_sprintf ("request_body", "Body={ \"contents\":[ { \"parts\":[{\"text\": \"hello?\"}]} ] }");
    web_add_header("Content-Type","application/json");
    llm_request("Gemini_Chat_Completion",
         "URL=https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=<API_KEY> ",
         "AIModel=gemini",
          lr_eval_string("{request_body}"),
          LAST);

### Anthropic model

Copy code

    char * bodyFormat = "Body={\"model\": \"claude-sonnet-4-6\", \"stream\": false, \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}],\"max_tokens\": 1024}";
    web_add_header("Content-Type","application/json");
    web_add_header("x-api-key","<API_KEY>");
    web_add_header("anthropic-version","2023-06-01");
    lr_param_sprintf("request_body_anthropic", bodyFormat, lr_eval_string("{question}"));  // here we have a parameter named "question" set in the script
    llm_request("Anthropic_Chat_Completion",
            "URL=https://api.anthropic.com/v1/messages",
            "AIModel=anthropic",
            lr_eval_string("{request_body_anthropic}"),
            LAST);

### Custom model

Copy code

    lr_param_sprintf ("request_body", "Body={\"messages\": [{\"role\": \"user\", \"content\": \"What is 2+2?\"}], \"stream\": false}");
    web_add_header("Content-Type","application/json");
    web_add_header("Authorization","Bearer <API_KEY>");
        llm_request("chat_completion",
            "URL=https://my-custom-llm-api.com/v1/chat/completions",
            "AIModel=custom",
            "RequestTokens=usage.prompt_tokens",
            "ResponseTokens=usage.completion_tokens",
            lr_eval_string("{request_body}"),
            LAST);

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: