Large Language Model Selection and Deployment: How to Use Long-Context Processing and Tool Calling
This guide explains how to evaluate whether a task is suitable for a large language model, compare rules-based and model-based solutions, test long-context reliability, and build safer tool-calling workflows with parameter validation, permission controls, retries, and troubleshooting practices.
1. Evaluate the Task Before Selecting a Model
Large language models are well suited to tasks with variable input formats and rules that are difficult to enumerate. Use the following three questions for an initial assessment:
- Are the inputs diverse enough that fixed rules cannot cover them effectively? For example, customer support tickets can vary widely in wording, making models suitable for intent classification.
- Can the output be verified quickly? Classification results can be reviewed through sampling, while contract risk findings can be checked against the original text item by item.
- Is the cost of errors acceptable? Errors in marketing copy may have limited impact, while errors involving financial amounts or medical advice can be much more serious. These tasks require a human review step.
When the first condition is met and the other two are adequately addressed, the model can handle most of the work. If the output cannot be verified quickly, add verification mechanisms before deployment. If the cost of errors is unacceptable, retain human confirmation.
When selecting an approach, compare the following three options:
| Approach | Cost | Output controllability | Response latency | Suitable scenarios |
|---|---|---|---|---|
| Rules engine | High development cost; maintenance becomes more difficult as rules increase | High; behavior is predictable | Low | Decisions based on fixed formats and explicit rules |
| Large language model | Billed according to usage | Medium; outputs are probabilistic | Relatively high | Tasks involving varied wording and semantic understanding |
| Large language model with human review | Model usage cost plus labor cost | Relatively high; errors can be intercepted | High | Tasks where errors have significant consequences |
For a specific task, record the following metrics:
- Task type: Classification, extraction, generation, question answering, or tool calling.
- Input variability: How much the wording varies among inputs of the same type.
- Cost of errors: The potential loss and scope of impact caused by incorrect results.
- Verifiability: Whether a program or human can quickly determine whether the output is correct.
- Latency and cost: Response time per request and cost per 1,000 calls.
- Sampling review metrics: For example, the number of items reviewed each week and the resulting error rate.
2. Understanding the Limits of Long-Context Processing
Vendors use different definitions for the term “context window.” It may refer to the maximum input length accepted by an interface, or to the range of content the model can use reliably. The former is an interface limit; the latter must be measured independently. A model’s ability to accept a long document does not mean it can accurately answer questions about details located in the middle of that document.
Use the following three steps to evaluate long-context capabilities.
Step 1: Confirm Input Limits and Billing
Check the official documentation for the interface’s input limits and billing method, and record the model version used in the test.
Step 2: Test Information Positioning with Your Own Data
Place the same key fact at the beginning, middle, and end of a document. Ask the corresponding question in each case and record the accuracy. If accuracy for information in the middle is significantly lower than for information at either end, the model’s use of the middle portion of long inputs is unstable. In that case, use segmentation or retrieval instead.
Step 3: Decide Whether to Segment the Document
If the task requires understanding the structure of the entire document—for example, analyzing relationships between contract clauses—you can provide the complete document to the model, but you should perform sampling-based validation.
If the task only requires finding a small number of relevant sections, a more reliable approach is to retrieve the relevant passages first and then provide those passages to the model.
A sample long-context evaluation set is shown below:
| ID | Document length | Location of key information | Question | Expected answer |
|---|---|---|---|---|
| LT-01 | Approximately 20,000 Chinese characters | Beginning | How many days is the delivery period specified in the contract? | Provide the number of days based on the first article of the main text |
| LT-02 | Approximately 20,000 Chinese characters | Middle | Which exemption cases are listed in Appendix 2? | List all items without omissions |
| LT-03 | Approximately 20,000 Chinese characters | End | What is the date of the latest revision? | Match the revision record at the end of the document |
Key long-context metrics include:
- Key information recall: Whether the model can find the required information.
- Factual consistency: Whether the answer is consistent with the source document.
- Unsupported answer rate: The proportion of answers that contain information not present in the document.
3. Tool Calling and Prompt Constraints
The basic tool-calling workflow is as follows: the model generates a tool name and parameters based on the user’s request, the application validates and executes the call, and the result is returned to the model so it can generate the final response. The model is responsible only for expressing the intended tool call. Execution permissions must remain under program control.
1. Put Parameter Validation in Code
Do not rely on the model to produce valid parameters. Each tool should define parameter types, required fields, and allowed value ranges. After receiving the model’s output, the application should first parse the JSON and validate it against the schema.
If validation fails, reject the call and return the error information to the model so it can retry. Set a maximum retry count to prevent unbounded attempts.
2. Apply the Principle of Least Privilege
- Expose tools through an allowlist. The model may call only the tools on that list.
- Use read-only access by default. Write, delete, transfer, and message-sending operations should require user confirmation or human approval.
- Grant the account used by each tool only the minimum permissions required to complete the task.
3. Configure Timeouts and Retries
- Set a timeout for every tool call. When a timeout occurs, return a clear failure message.
- Retry only idempotent operations. Query operations can generally be retried. For operations such as creating an order, first check whether the order has already been created before retrying.
- Increase the retry interval progressively and set a maximum number of attempts to prevent resource consumption caused by looping calls.
4. Troubleshooting Procedure
When a tool call fails, investigate the issue in the following order:
- Check whether the model called a tool that does not exist. If so, verify that the tool definition was passed correctly and that its description is clear.
- Check whether the parameters can be parsed as JSON. If not, verify that the prompt includes a clear example of the required output format.
- Check whether the parameter types and values comply with the schema. If failures occur intermittently, analyze failed samples to determine whether they are concentrated in a particular type of input.
- Check the tool’s own return value and distinguish between an error from the tool and a model misinterpretation of the returned result.
- Check for duplicate or looping calls. If they occur, limit the number of calls in the prompt and enforce a hard limit in code.
- Record the complete call chain, including the input, tool name, parameters, return value, and final answer, so that issues can be reproduced.
Common symptoms and recommended actions include:
| Symptom | Common cause | Recommended action |
|---|---|---|
| The model does not call the tool | The tool description is unclear, or the question does not require a tool | Improve the tool description and specify when it should be called |
| Parameters are missing | Required fields were not explained in the prompt | Mark required fields in the schema and state them clearly in the prompt |
| Parameter format errors | Dates or amounts are expressed inconsistently | Normalize formats at the tool layer before validation |
| Tool timeout | The downstream service responds slowly | Adjust the timeout and provide a fallback response |
| The answer does not match the tool result | The model did not correctly reference the returned content | Require the answer to cite its basis and verify results through sampling |
5. Prompt Constraints
The system prompt should specify the following:
- Role
