Network operators are migrating their networks to IP over WDM architectures. In such multilayer networks, it is necessary to define mechanisms that efficiently use the resources available from both layers in a coordinated manner. Thanks to the development of the control plane (GMPLS), it is feasible to both set up and tear down lightpaths automatically. Thus the WDM layer can now adapt its configuration to both meet a set of resource constraints and fulfill some predefined quality-of-service (QoS) requirements. To this end, we define a multilayer traffic engineering algorithm that efficiently manages the resources from both layers. This algorithm, which is based on Bayesian decision theory, finds the optimal number of label-switched paths that must be switched over the optical and electronic domains. Such an optimal value is found by trading off the QoS required by the users and the limited availability of network resources.