Collaborating Large and Small LLMs in Software Development: From Instruction Boundaries to Concurrency Locking

    This article examines how large and small LLMs differ in task understanding, why vague instructions create implementation risk, and how to specify concurrency control precisely. Using a case where archives depend on data records, it compares locking strategies, provides implementation guidance and testing practices, and outlines a practical division of labor between larger models for design and review and smaller models for specification-driven implementation.

    1. Differences in Model Scale and Task Understanding

    In software development, model scale mainly affects two capabilities: the accuracy of executing explicit instructions and the ability to infer implicit intent.

    Smaller models respond quickly and cost less when instructions are explicit and boundaries are complete. They are suitable for batch generation, format conversion, boilerplate code, and unit tests. Their weakness is insensitivity to unstated constraints. If a premise that the developer assumes by default is not written into the instruction, the model often completes the task literally. The result may pass tests but fail the design intent.

    Larger models can infer goals from context. Given a business description, they are more likely to determine which data needs protection, which exceptions must be handled explicitly, and which operations compete with each other.

    The two types of models may score similarly on general benchmarks, but in real engineering tasks the difference usually appears in “constraints that are not written down.” Therefore, when evaluating a model, do not look only at scores; also examine what assumptions it makes under ambiguous requirements.

    2. Why Instructions Need to Cover Boundary Conditions

    Consider concurrency control as an example. The requirement can be written as: “When operating on an archive, apply an exclusive lock to ensure data safety.” This sentence is complete at the business level, but at the implementation level it has several reasonable interpretations:

    • Lock the folder ID that owns the archive;
    • Lock the primary key ID of the data record;
    • Use optimistic locking with a version number for validation.

    The three approaches differ in code complexity, concurrency semantics, and test coverage. If the model receives no explicit constraint, it will choose one of them.

    The table below lists common deviations that ambiguous instructions may cause:

    Ambiguous instruction Possible interpretation Risk
    Apply an exclusive lock Lock any related table or folder Wrong lock scope and wrong protected object
    Prevent concurrency conflicts Add only an in-memory lock at the application layer Fails in multi-process deployments
    Check existence before the operation Check only once before locking The record may be deleted while waiting for the lock
    Ensure transaction safety Unclear transaction scope Lock held too long or released too early

    The closer an instruction is to implementation details, the less the model needs to guess.

    3. Case Study: Concurrency Control for Archives Attached to Data

    The business relationship is as follows: each archive belongs to one data record. Reads and writes to the archive must be mutually exclusive with deletion of that record; otherwise, an archive write may succeed while its owning record has already been deleted, creating dangling data.

    Option A: Lock the folder ID that owns the archive.
    The problem with this approach is that the protected object does not match the real dependency. The data record may be deleted by another session while the lock is held, and the archive operation will still continue. In addition, different folders under the same data record cannot see each other, so the protection scope is incomplete.

    Option B: Lock the primary key ID of the data record.
    The archive operation and data deletion compete for the same row lock. The delete operation waits until the archive operation commits, or the archive operation finds after acquiring the lock that the record no longer exists and returns an error directly. The code path needs only one existence check, so the logic is simpler.

    The following example assumes that the database connection object supports a transaction context manager, and the parameter placeholder style depends on the driver:

    1. class RecordNotFound(Exception):
    2. pass
    3. def write_archive(conn, record_id: int, archive_payload: dict) -> None:
    4. with conn.transaction():
    5. # 1. Acquire a row lock on the primary key, mutually exclusive with deletion
    6. row = conn.execute(
    7. "SELECT id FROM data_record WHERE id = %s FOR UPDATE",
    8. (record_id,),
    9. ).fetchone()
    10. # 2. Recheck after locking. While waiting for the lock, the record may have been deleted
    11. if row is None:
    12. raise RecordNotFound(record_id)
    13. # 3. Write the archive in the same transaction; the lock is released when the transaction ends
    14. conn.execute(
    15. "INSERT INTO archive (record_id, content) VALUES (%s, %s)",
    16. (record_id, archive_payload["content"]),
    17. )

    Keep the following points in mind during implementation:

    1. Recheck after locking. The result read before locking may already be stale, so you must check whether the record exists after acquiring the lock.
    2. Precise index conditions. Locking by primary key can lock a single row. If the query condition does not use an index, the database may take a broader lock and affect other operations.
    3. Lock in a fixed order. When operating on multiple records at the same time, lock them in ascending primary key order to avoid deadlocks caused by two transactions waiting in opposite orders.
    1. def lock_records_in_order(conn, record_ids):
    2. for rid in sorted(set(record_ids)):
    3. conn.execute(
    4. "SELECT id FROM data_record WHERE id = %s FOR UPDATE",
    5. (rid,),
    6. )
    1. Set a lock wait timeout. Prevent a long transaction from holding a lock for a long time and blocking all other requests.
    2. Do not call external services inside the lock. Network request latency is unpredictable and extends lock holding time.

    4. A Requirements Checklist for Models

    A runnable instruction should cover at least the following seven items:

    1. Lock target: precise to the table and field, for example, “the id field of the data_record table.”
    2. Lock scope: single row, multiple rows, or table-level.
    3. Transaction boundary: from which step to which step, and when the lock is released.
    4. Existence check: check once before locking, and recheck once after locking.
    5. Exceptions and rollback: what errors to return for record not found, timeout, and deadlock, and how the transaction rolls back.
    6. Concurrency scenarios: explicitly list the combinations to guard against, for example, “deleting a data record and writing an archive at the same time.”
    7. Test requirements: include race tests, timeout tests, and regression tests.

    A qualified instruction example is shown below:

    1. Apply an exclusive lock for archive writes. The lock target is the id field (primary key) of the data_record table.
    2. Acquire the lock inside the transaction and release it after commit or rollback.
    3. After acquiring the lock, recheck whether the record exists; if not, raise RecordNotFound.
    4. When operating on multiple records at the same time, lock them in ascending id order.
    5. If the lock wait exceeds the configured timeout, return a timeout error and roll back the transaction.
    6. Tests must cover: concurrent delete and write, timeout, and multi-record lock ordering.

    This text uses no words that require the model to guess, so a small model can execute it directly.

    5. Division of Labor Between Large and Small Models

    In real projects, the two types of models are suited to different stages:

    • Larger models: solution design, boundary identification, code review, and concurrency risk analysis.
    • Smaller models: implementation according to specification, batch modifications, generating test cases, and unifying code format.

    The recommended collaboration workflow is as follows:

    1. The larger model produces a design draft from the business description, listing candidate solutions and their trade-offs.
    2. The developer confirms the lock target, transaction boundary, and exception strategy, forming a specification document.
    3. The smaller model implements the code according to the specification document.
    4. The larger model reviews the implementation, focusing on omissions outside the specification.
    5. Tests verify behavior.

    How can you judge whether a model is suitable for design review? Provide a technical comment with a clear viewpoint and ask the model to answer four questions in order: What is the core argument? Is the argument valid? What is the principle that makes it valid? Where are its applicable boundaries? A model that can accurately extract the argument and point out exceptions is better suited to review work. If the output always compromises among different views or avoids making a judgment on the conclusion, its reliability on this type of task is insufficient.

    6. Verification and Testing

    Concurrency-related changes are hard to fully cover through code review alone; tests are needed to prove correct behavior.

    • Race test: open two sessions, one deletes a data record, and the other writes an archive. Assert that one side fails and that no archive points to a deleted record.
    • Timeout test: one session holds the lock without committing; the other session’s write should return a timeout error after the timeout period and leave no incomplete data.
    • Deadlock test: two sessions operate on two records in opposite orders. After locking in primary key order, no deadlock should occur.
    • Regression test: retain all existing test cases for the archive business and confirm that normal-path behavior has not changed.

    Tests can use multithreading or multiple connections to simulate contention, and after each run check whether orphaned archive records exist in the database.

    7. Practical Notes

    • Different databases vary widely in lock implementation and isolation levels; behaviors such as gap locks should be based on the documentation of the database in use.
    • Finer lock granularity usually improves concurrency, but you need to confirm that the index can precisely locate the target rows.
    • Every constraint in an instruction should be verifiable by tests; constraints that cannot be verified are difficult to guarantee in actual execution.