Building Controllable AI Customer Support That Executes Business Tasks with Code

    Learn how to move AI customer support beyond answering questions and into reliable business execution. This article explains how to define business rules, validate model outputs, manage conversation state, enforce confirmation and authorization, handle uncertain API results, and evaluate whether users actually complete their tasks.

    1. From Answering Questions to Completing Tasks

    Many AI customer support systems retrieve information and then generate an answer. This approach works well for explaining service policies, describing business rules, and answering common questions. It does not, by itself, enable the system to carry out business operations.

    Consider a user who says: “I just placed an order, but I entered the wrong shipping address. Can you change it?”

    Accurately explaining when an address can be changed does not resolve the request. To complete it, the system must at least:

    1. Verify the user’s identity and permission to modify the order.
    2. Identify the order the user wants to change.
    3. Check whether the order is still eligible for an address change.
    4. Collect and validate the new shipping address.
    5. Show the proposed change and obtain confirmation.
    6. Call the business API to apply the change.
    7. Report success, failure, or an unresolved status based on the API result.

    An AI support system that can complete tasks needs more than a reassuring “I’ll take care of that.” It must connect natural-language interaction to a reliable execution workflow.

    The core principle is:

    The model handles understanding and communication, application code enforces rules and execution, and business systems provide authoritative facts and results.

    Clear boundaries between these responsibilities make the system easier to control, test, and maintain.

    2. Why Use a Code-Driven Approach?

    Visual workflow orchestration and autonomous agents are common implementation options, but they suit different situations.

    Visual workflows make processing steps easy to understand and work well for clearly bounded processes with relatively few paths. However, branches can multiply when users add information, change their requests, ask unrelated questions, or cancel an operation at any point. The difficult part is often not calling an API, but managing state changes across a multi-turn conversation.

    Autonomous agents offer flexibility by letting the model choose tools, organize steps, and respond to feedback. But system behavior becomes less deterministic if the model also decides authorization, execution conditions, and whether an operation succeeded.

    A code-driven approach does not reject either option. Instead, it places critical constraints in inspectable program logic:

    • Configuration and permission rules determine which tasks may be automated.
    • Field requirements determine which parameters must be present.
    • Risk levels determine which operations require confirmation.
    • Explicit routing determines which API can be called.
    • Business-system responses determine whether a task is complete.

    For teams with development expertise that need to integrate existing business systems, this approach supports version control, automated testing, troubleshooting, and auditing.

    It also creates engineering responsibilities. Teams still need to maintain integrations, monitor runtime behavior, and manage security. A small number of files does not mean there is no engineering overhead.

    3. Start with One Narrow Business Task

    For an initial implementation, choose one task with clear boundaries, a mature API, and manageable risk. Examples include checking an order’s status or changing a shipping address before fulfillment begins.

    Define six things for each task:

    1. What goal is the user trying to achieve?
    2. What information is required?
    3. What permissions must the user have?
    4. What conditions must be met before execution?
    5. Is explicit user confirmation required?
    6. What result counts as completion?

    For a shipping-address change, the business contract might look like this:

    Item Definition
    Business goal Change the shipping address for an order the user is authorized to modify
    Required information Target order, new address, and necessary contact details
    Authorization condition The currently authenticated user has permission to modify the order
    State condition The order is still in a stage that permits address changes
    Confirmation requirement Show the target order and new address, then obtain explicit confirmation
    Evidence of success The update API returns a completed business result
    Exception handling Route state conflicts, API failures, and uncertain results to the appropriate handling path

    These rules should come from existing business policies and API constraints, not from the model’s guesses.

    4. Organize a Minimal Prototype Around Two Core Files

    A “two-file approach” describes how to organize the application’s core logic. It does not mean a production system needs only two files.

    Business Rules File

    This file defines intents, required fields, validation requirements, confirmation policies, and execution entry points. It should be version-controlled, reviewable business configuration—not a catch-all collection of natural-language prompts.

    The following is an illustrative custom configuration, not a vendor-specific API format:

    1. Business task: Change shipping address
    2. Required fields:
    3. - Order identifier
    4. - New address
    5. - Contact name
    6. - Contact details
    7. Authorization requirements:
    8. - The current user may modify the target order
    9. Pre-execution checks:
    10. - The order status permits an address change
    11. Confirmation required:
    12. - Yes
    13. Execution action:
    14. - Call the address-update adapter function
    15. Exception routing:
    16. - Unauthorized: Reject the operation
    17. - Ineligible order status: Explain the reason
    18. - Uncertain result: Query the operation result or hand off to a human

    The authorization requirement in configuration is only a declaration. Actual authorization must still be enforced on a trusted server.

    Conversation Execution Program

    The execution program reads configuration, loads session state, calls the model to interpret input, validates parameters, and drives business execution.

    It should not allow the model to produce arbitrary endpoint URLs or executable code. Instead, it should map the business task identified by the model to a preregistered handler. This preserves conversational flexibility while limiting the available actions.

    As the system grows, state storage, API adapters, security controls, and tests can be separated into additional modules. Two files are a starting point, not a permanent architectural requirement.

    5. Treat Model Output as a Candidate Interpretation

    Natural language includes omissions, ambiguity, and dependencies on context. A model can turn a user’s message into a structured candidate interpretation:

    1. Candidate task: Change shipping address
    2. Extracted information:
    3. Order identifier: Not specified
    4. New address: City, district, street...
    5. Contact name: Not specified
    6. User action:
    7. Requests a change
    8. Ambiguity:
    9. Yes; the target order is unclear

    The program then checks:

    • Whether the candidate task is on the allowlist.
    • Whether the output matches the required structure.
    • Whether field types and lengths are valid.
    • Whether parameters conflict with existing state.
    • Whether the current stage permits entering this task.

    Correct formatting does not establish factual accuracy. A model may extract an order number from a message, but only a trusted business system can establish whether the order exists and whether the user may access it.

    When intent is unclear, ask for clarification rather than forcing the request into a branch. Even apparently high model confidence cannot replace identity verification, business checks, or user confirmation.

    6. Manage Multi-Turn Tasks with a State Machine

    Controllable customer support needs explicit business state, not just a chat history.

    A general state model is:

    1. Awaiting request
    2. → Collecting information
    3. → Validating conditions
    4. → Awaiting confirmation
    5. → Executing task
    6. → Completed / Failed / Result unresolved

    Each state should define which events are permitted. While awaiting confirmation, for example, the user may confirm, cancel, or change information. Changing a critical field must invalidate the previous confirmation.

    Useful state data includes:

    • Current user and session identifiers.
    • The active business task.
    • Collected fields and their sources.
    • Current state and state version.
    • The operation summary awaiting confirmation.
    • A unique operation identifier.
    • The latest trusted business result.

    Maintain this state on the server. Do not rely solely on the model’s conversational memory.

    Rapid consecutive messages or concurrent requests also require concurrency controls to prevent older messages from overwriting newer state. State-version checks can reject updates based on an unexpected version, allowing processing to resume after reloading the current state.

    7. Make Confirmation an Actual Execution Gate

    For operations with side effects, confirmation must be more than a line of dialogue.

    Generate a clear operation summary, such as:

    You are about to change the shipping address for the order ending in … to …, with … as the contact. Confirming will submit the change. Would you like to continue?

    Bind the confirmation record to the specific task, target object, and parameter version. If the order, address, or another critical field changes after confirmation, obtain confirmation again.

    A previous “okay” must not become indefinite authorization. Nor should an affirmative answer to an ordinary question be mistaken for confirmation of a business operation.

    Immediately before execution, recheck the necessary business conditions. An order may become ineligible for modification while the user is reviewing the confirmation, and the execution program must handle that change.

    Operations involving money, account security, or other high-risk actions require appropriate additional identity verification or human handling. Natural-language confirmation alone is not sufficient.

    8. Execute Through APIs and Handle Uncertainty Correctly

    The following pseudocode illustrates the main processing logic, omitting specific API and storage implementations:

    1. function handle_message(identity, session, message):
    2. state = load_session_state(identity, session)
    3. interpretation = parse_user_input(message, necessary_context_from(state))
    4. if interpretation_has_invalid_structure:
    5. return clarification_question
    6. if user_requests_cancellation:
    7. close_current_task()
    8. return cancellation_message
    9. update_candidate_fields_and_validate_sources_and_formats()
    10. if task_or_target_is_unclear:
    11. save_state()
    12. return clarification_question
    13. if required_information_is_missing:
    14. save_state()
    15. return request_missing_information
    16. check_authorization_and_business_conditions()
    17. if confirmation_required_and_current_parameters_not_confirmed:
    18. save_confirmation_summary()
    19. return confirmation_prompt
    20. recheck_necessary_conditions_before_execution()
    21. operation_id = get_or_create_unique_id_for_current_operation()
    22. result = call_preregistered_business_function(parameters, operation_id)
    23. save_trusted_result_and_updated_state()
    24. return generate_response_from_result(result)

    API execution must distinguish at least three outcomes:

    • Definite success: Tell the user the task is complete.
    • Definite failure: Explain the reason that can be disclosed and offer a next step.
    • Uncertain result: Do not claim success, and do not immediately resubmit the operation.

    Network timeouts require particular care. A timeout means the caller did not receive a result in time; it does not prove that the business system did not execute the request. For write operations, use idempotency controls, result queries, or human verification to prevent duplicate execution.

    A unique operation identifier is useful only if the server actually enforces idempotency. Generating an identifier alone does not prevent duplicate operations.

    9. Separate Knowledge Answers from Business Execution

    A knowledge base remains valuable for explaining rules, but it should not serve as a source of live transaction data.

    “What are the conditions for changing a shipping address?” can be answered from reviewed business documentation. “Can I still change the address on my order?” requires checking the order’s current status.

    If a user asks a policy question during a task, the system can answer it and then return to the original workflow. Whether to retain the task depends on its time sensitivity. A long pause or a change in critical conditions should trigger renewed validation.

    Response generation must also remain grounded in business facts. Use wording such as “The address has been changed” only when the API explicitly reports completion. If the business system has merely accepted a request, say “The request has been submitted and is being processed.” Acceptance is not completion.

    10. Design Security, Logging, and Human Handoff Together

    User messages, knowledge sources, and natural-language API responses can all contain untrusted instructions. Text asking the system to “ignore the rules” must not bypass authorization or expand tool permissions.

    Security boundaries should include:

    • An allowlist of executable business tasks.
    • Server-side identity and resource authorization checks.
    • Parameter validation and masking of sensitive output.
    • Credential management in a trusted runtime environment.
    • Data isolation between sessions.
    • Minimal transmission and storage of sensitive information.
    • Additional authorization requirements for high-risk actions.

    Logs should record the business task, state transitions, parameter-validation results, API results, and operation identifier. Avoid retaining full addresses, contact details, or other sensitive data without a necessary reason.

    Human handoff should provide a privacy-filtered summary covering the user’s goal, collected information, completed steps, and the reason processing is blocked. In particular, make clear whether a write operation has already been attempted or executed so that a human agent does not submit it again.

    11. Measure End-to-End Business Completion

    Evaluation should go beyond natural-sounding answers and accurate intent recognition. It should also examine:

    • Whether the user actually completed the intended task.
    • Whether missing information was collected effectively.
    • Whether any write operation occurred without confirmation.
    • Whether duplicate execution occurred.
    • Whether the result shown to the user matched business-system facts.
    • Whether failures could be recovered from or handed off smoothly.

    Tests should cover both normal paths and situations such as parameter changes, cancellation, API timeouts, insufficient permissions, state conflicts, concurrent messages, and malicious instructions.

    Begin deployment with read-only queries, then gradually enable low-risk write operations. Expand the supported business scope as the system becomes stable, rather than giving the model unrestricted execution capabilities from the outset.

    Conclusion

    Building AI customer support with code is not primarily about minimizing file count. It is about clear responsibilities, explicit state, and trustworthy execution.

    The language model handles understanding and communication. Business rules define what is permitted. A state machine organizes multi-turn interaction. API adapters carry out operations. Authorization, confirmation, idempotency, and auditing protect the execution boundary.

    When these components form a complete workflow, customer support can move beyond plausible answers and reliably complete user tasks within explicit constraints.