Best Practices for Complex Reasoning and 3D Code Generation with LLMs

    This article explores practical strategies for evaluating complex reasoning tasks and generating 3D code using large language models (LLMs). It covers standardized testing methodologies for constraint-based math and geometry problems, modular task decomposition for 3D game development, troubleshooting coordinate system misalignments, and establishing automated cloud-based verification loops. By implementing these practices, developers can ensure the stability and reliability of LLM outputs in production environments.

    1. How to Test Complex Reasoning Problems

    Many teams evaluate a model by checking if a single response is correct to decide if it is usable. This approach often leads to misjudgments. Model outputs are influenced by versions, temperature parameters, and whether deep thinking is enabled. The same problem can yield completely different results under different settings. Therefore, when testing, record the following information: model name and version, test date, temperature and top_p parameters, deep thinking status, original prompt, and evaluation criteria. Only with complete information can results be reproduced by others.

    When testing the same problem, it is recommended to run it multiple times and count the pass rate rather than relying on a single result. The pass rate reflects the model’s stability on specific problem types; a single correct answer does not indicate reliable capability.

    1.1 Multi-constraint Extrema Solving

    Take the example of “finding the minimum integer that satisfies a square number condition within a specific interval.” The difficulty of such problems usually lies in the constraints. The most common areas where models make mistakes include:

    Problem Type Common Constraints Common Errors Self-Check Points
    Extrema solving in an interval Whether endpoints are included, whether negative numbers are allowed Ignoring negative solutions, misjudging open/closed endpoints Check endpoints one by one, verify if negative numbers meet the problem’s requirements
    Square number conditions Whether 0 is included, whether 0 counts as a square number Missing 0 or misjudging 0 as invalid Substitute 0 into the condition to confirm consistency
    Conclusion validation Whether the final answer truly satisfies all constraints Only providing the conclusion without back-substitution Substitute the answer back into the original equations for item-by-item verification

    The key to such problems is whether the model can treat implicit constraints as actual constraints. If the model only focuses on the main problem statement, it will make errors on negative numbers or boundary values.

    1.2 Permutations and Inclusion-Exclusion

    Take the example of “finding the total permutations of 8-digit numbers formed from a given set of digits where the first digit cannot be zero.” The standard approach is to calculate the total permutations and subtract those with a leading zero. This requires the model to understand both permutation counting and the inclusion-exclusion principle while maintaining state consistency during a longer derivation process.

    Models commonly make three types of errors on these problems. First, calculating the total permutations but forgetting to exclude cases with a leading zero. Second, treating permutations with duplicate digits as unique. Third, having inconsistent conditions during derivation, such as stating that the first digit cannot be zero initially, but including it during the calculation.

    During verification, you can require the model to explicitly output the two intermediate results: “total permutations” and “permutations with a leading zero,” and then subtract to get the answer. Intermediate results make the location of errors immediately visible.

    1.3 Spatial Geometry and Immersion Problems

    Problems like “calculating the water level rise after submerging a corner-clipped cube in a water-filled container” test both geometric volume calculation and physical state judgment. The core formula is: when fully submerged, the displaced water volume equals the object’s volume. If the container has vertical walls and a constant cross-sectional area, the water level rise is:

    Δh = V / S

    Where V is the volume of the submerged part of the object, and S is the available cross-sectional area of the water surface inside the container.

    For example, a corner-clipped cube has a volume of 950 cm³, the container base area is 200 cm², and the object is fully submerged in water. The water level rise is 950 ÷ 200 = 4.75 cm.

    This formula has application boundaries. First, if the object floats, the displaced water volume is determined by the object’s mass and the water’s density, not its own volume. Second, if the object is only partially submerged, only the volume of the submerged part should be calculated. Third, if the container is not cylindrical, the cross-sectional area S will change with height, requiring integration or segmented calculation. Models often make mistakes at these boundaries, so the prompt should clearly specify these three points.

    2. Task Decomposition for 3D Single-File Games

    Using a single prompt to ask a model to generate a complete 3D game usually has a very low success rate. A more reliable approach is to decompose it into multiple modules, generate and validate each module separately, and finally integrate them.

    Below is a decomposition table that can be used directly:

    Module Acceptance Criteria Common Failure Symptoms
    Rendering Initialization Scene visible on page load, no console errors, stable frame rate Black screen, missing lighting, incorrect resource paths
    Terrain Generation Same seed generates same terrain, height range within expectations Terrain differs on refresh, or large empty spaces appear
    Character Controller Normal movement in all directions, sufficient jump height, blocked by walls Cannot jump, movement direction opposite to input
    Entity AI Can track player, attack range matches visual feedback Entity stands still, or attack distance is visibly off
    VFX and Particles Projectiles spawn, move, and are correctly destroyed VFX not showing, or projectiles remain permanently
    Collision Detection Stands on ground without sinking, wall clipping test fails Character clips through walls, or jitters upon landing

    Each module must pass acceptance before moving on to the next. This way, if an error occurs, it can be quickly located to a specific module instead of searching line by line through hundreds of lines of code.

    It is worth noting that different models often exhibit capability biases. Some models have solid logic but weak rendering VFX; others have beautiful visuals but flawed physics. After decomposition, you can select the better-performing model for each module individually, rather than relying on a single model to complete all the work.

    3. Troubleshooting Coordinate System Misalignment

    Coordinate system misalignment is the most common and hardest to debug issue in 3D code generation. Symptoms usually include incorrect screen orientation when rotating the view, mismatched character movement and camera direction, or misaligned block and character positions.

    3.1 Coordinate Transformation Chain

    3D rendering typically involves three transformations: model space to world space, world space to camera space, and camera space to clip space. If the order is written incorrectly, the scene will be misaligned. In column-major matrix libraries, the correct formulation is:

    clip = P × V × M × vertex

    Where M is the model matrix, V is the view matrix, and P is the projection matrix. Many generated codes reverse the multiplication order or use different conventions across modules, resulting in a scene that looks “skewed.”

    3.2 Camera Orientation Calculation

    The view matrix relies on the lookAt function. Using a right-handed coordinate system as an example, the standard calculation steps are:

    1. function lookAt(eye, target, up):
    2. f = normalize(target - eye) // View direction
    3. r = normalize(cross(f, up)) // Camera right direction
    4. u = cross(r, f) // Camera up direction
    5. // The first three rows of the view matrix are r, u, -f, with translation terms -dot(r, eye), -dot(u, eye), dot(f, eye)

    If the up vector is parallel to the view direction, the cross product will yield a zero vector, causing the camera to suddenly flip. A common issue in generated code is the failure to handle this situation.

    3.3 Voxel Coordinate Conversion

    In voxel worlds, world coordinates and voxel indices need to be converted back and forth. The common implementation is:

    1. voxelIndex = floor((worldPos - origin) / voxelSize)
    2. voxelCenter = origin + (voxelIndex + 0.5) * voxelSize

    Many errors occur in the rounding method. If coordinates are rounded directly instead of voxel centers, the character will frequently jitter when standing on block boundaries. If the origin offset is inconsistent between terrain generation and collision detection, the character will appear to sink into the ground or float in the air.

    3.4 Troubleshooting Flow

    When troubleshooting, follow this sequence:

    1. Place a marker at the world origin (0, 0, 0) and (1, 0, 0) respectively, and check if their positions on the screen meet expectations.
    2. Fix the camera position, only change the character coordinates, and observe if the movement direction matches the camera.
    3. Check if terrain generation and collision detection use the same origin and voxelSize.
    4. Print key matrix elements and compare them with manual calculation results.
    5. If the issue only occurs at specific viewing angles, check if the up vector is parallel to the view direction.

    Following this sequence, most coordinate issues can be located within a few tests.

    4. Constraining Models with Chain of Thought and Self-Check Prompts

    In complex reasoning problems, asking the model to output its reasoning process is usually more reliable than asking for a direct answer. However, this approach is not always effective and needs to be adjusted based on the model type.

    For models with weaker reasoning capabilities, requiring step-by-step derivation can significantly reduce skipped steps. For strong reasoning models that already perform internal reasoning, additional reasoning text may not help much, while increasing token consumption and response time. It is recommended to conduct comparative tests on the same set of problems, comparing pass rates and costs before deciding whether to adopt this approach.

    You can use the following prompt template directly:

    1. Please list all constraints in the problem first, and explain your reasoning for each.
    2. After completing the calculation, check against this list:
    3. 1. Does the result satisfy all constraints, including whether negative numbers are allowed and whether endpoints are included.
    4. 2. If the problem requires the first digit to not be 0, have the corresponding cases been excluded in the count.
    5. 3. If physical states are involved, have you determined submersion or floating, and whether it is fully immersed.
    6. Finally, output only the final answer and provide a one-sentence check conclusion.

    The checklist in the template should correspond to the problem type. Combinatorics problems should focus on boundaries and exclusion conditions, geometry and physics problems on state judgment and applicable conditions, and code problems on interface contracts and boundary inputs.

    5. Building a Cloud-Based Automated Verification Loop

    Relying solely on manual code review makes it difficult to detect runtime errors. The automated verification process can be: the model generates code, passes static checks, the HTML file is run in an isolated environment, loaded via a headless browser, and console logs, exception information, and key frame screenshots are collected and fed back to the model or developers.

    When building this loop, note the following points.

    First, permission isolation. The runtime environment should lack the ability to access the intranet or sensitive resources. Network access should use a whitelist, the file system should be read-only as much as possible, and limits on CPU, memory, and execution time should be set.

    Second, data privacy. The test code and prompts may contain business information. Before using third-party cloud services, confirm their data retention period, whether the data is used for training, and whether data deletion is supported.

    Third, cost control. Each run incurs computing costs. Concurrency should be limited, repeated test results should be cached, and the same code should not be repeatedly executed.

    Fourth, result judgment. Screenshot comparisons are easily affected by rendering details. It is recommended to write specific rules for judgment conditions, such as “no console errors” or “character completes a jump within 3 seconds,” rather than just checking if the screen “looks normal.”

    6. Pre-Launch Checklist

    Before using model outputs in a production environment, it is recommended to complete the following three checks:

    1. Every reasoning problem answer has been back-substituted or independently recalculated, with the model version and parameters from testing retained.
    2. Every module of the 3D code has passed its corresponding acceptance criteria, and coordinate mapping has been verified from at least two different perspectives.
    3. The automated verification environment has completed permission isolation, and data retention and cost limits have been confirmed.

    Additionally, quantitative metrics can be set. For example, allow progression to the next module only after a module’s acceptance rate reaches a predefined threshold. The threshold should be determined by the team based on business risk, rather than directly following an empirical number.

    LLMs can help developers complete reasoning and coding tasks faster, but their outputs still require clear testing conditions, precise module boundaries, and repeatable inspection processes. Solidifying these aspects ensures that model results can be stably applied in real-world engineering.